zoukankan      html  css  js  c++  java
  • POJ 基本算法

      

    枚举 

    poj1753 (位运算+BFS)

    poj2965(同上)

    贪心

    Poj1328 ,poj2109雷人

    poj2586枚举五种情况

    分治法

    poj2524很裸的并查集

    递推

    poj2506大数

    f[n] = f[n-1] + 2*f[n-2]

    构造法

    Poj3295给出前缀式,判是否是重言式。枚举共32种情况

    模拟法
    poj1068  (用0表示,)用1表示,把括号的情况模拟出来,然后再统计。
    poj2632 插!这题 DBL!if..else..,无穷尽也。。。贴下代码吧 
    poj1573, 简单模拟 
    poj2993, 
    poj2996 两题类似,2993处理时用STL里的istringstream要简单

                                                                  

                                                                                                          

                                                                                                                                                                         

    POJ 2632 (感谢提供数据的那位大牛 Orz)

    View Code
      1 #include <iostream>
    2 #include <cstring>
    3 #include <cstdio>
    4
    5 using namespace std;
    6
    7 const int N = 1000;
    8
    9 struct robot {
    10 int x;
    11 int y;
    12 char dir;
    13 } rob[N];
    14
    15 int row, col, n, m;
    16
    17 int check(int a) {
    18 int i;
    19 for(i = 1; i <= n; i++) {
    20 if(i == a) continue;
    21 if(rob[i].x == rob[a].x && rob[i].y == rob[a].y) return i;
    22 }
    23 return 0;
    24 }
    25
    26 int go(int a, int step) {
    27 int i, ans;
    28 char d = rob[a].dir;
    29 if(d == 'E') {
    30 for(i = 1; i <= step; i++) {
    31 rob[a].x++;
    32 if(rob[a].x > row) return -1;
    33 ans = check(a);
    34 if(ans != 0) return ans;
    35 }
    36 } else if(d == 'W') {
    37 for(i = 1; i <= step; i++) {
    38 rob[a].x--;
    39 if(rob[a].x < 1) return -1;
    40 ans = check(a);
    41 if(ans != 0) return ans;
    42 }
    43 } else if(d == 'S') {
    44 for(i = 1; i <= step; i++) {
    45 rob[a].y--;
    46 if(rob[a].y < 1) return -1;
    47 ans = check(a);
    48 if(ans != 0) return ans;
    49 }
    50 } else {
    51 for(i = 1; i <= step; i++) {
    52 rob[a].y++;
    53 if(rob[a].y > col) return -1;
    54 ans = check(a);
    55 if(ans != 0) return ans;
    56 }
    57 }
    58 return 0;
    59 }
    60
    61 void change_L(int a, int b) {
    62 if(b == 0) return;
    63 if(b == 1) {
    64 if(rob[a].dir == 'E') rob[a].dir = 'N';
    65 else if(rob[a].dir == 'W') rob[a].dir = 'S';
    66 else if(rob[a].dir == 'N') rob[a].dir = 'W';
    67 else if(rob[a].dir == 'S') rob[a].dir = 'E';
    68 } else if(b == 2) {
    69 if(rob[a].dir == 'E') rob[a].dir = 'W';
    70 else if(rob[a].dir == 'W') rob[a].dir = 'E';
    71 else if(rob[a].dir == 'N') rob[a].dir = 'S';
    72 else if(rob[a].dir == 'S') rob[a].dir = 'N';
    73 } else {
    74 if(rob[a].dir == 'E') rob[a].dir = 'S';
    75 else if(rob[a].dir == 'W') rob[a].dir = 'N';
    76 else if(rob[a].dir == 'N') rob[a].dir = 'E';
    77 else if(rob[a].dir == 'S') rob[a].dir = 'W';
    78 }
    79 }
    80
    81 void change_R(int a, int b) {
    82 if(b == 0) return ;
    83 if(b == 1) {
    84 if(rob[a].dir == 'E') rob[a].dir = 'S';
    85 else if(rob[a].dir == 'W') rob[a].dir = 'N';
    86 else if(rob[a].dir == 'N') rob[a].dir = 'E';
    87 else if(rob[a].dir == 'S') rob[a].dir = 'W';
    88 } else if(b == 2) {
    89 if(rob[a].dir == 'E') rob[a].dir = 'W';
    90 else if(rob[a].dir == 'W') rob[a].dir = 'E';
    91 else if(rob[a].dir == 'N') rob[a].dir = 'S';
    92 else if(rob[a].dir == 'S') rob[a].dir = 'N';
    93 } else {
    94 if(rob[a].dir == 'E') rob[a].dir = 'N';
    95 else if(rob[a].dir == 'W') rob[a].dir = 'S';
    96 else if(rob[a].dir == 'N') rob[a].dir = 'W';
    97 else if(rob[a].dir == 'S') rob[a].dir = 'E';
    98 }
    99 //if(a == 3) printf("%d %d %d %c\n", b, rob[a].x, rob[a].y, rob[a].dir);
    100 }
    101
    102 int main() {
    103 //freopen("data.in", "r", stdin);
    104
    105 int t, a, b, i, flag, flag2;
    106 char ch;
    107 scanf("%d", &t);
    108 while(t--) {
    109 scanf("%d%d", &row, &col);
    110 scanf("%d%d", &n, &m);
    111 memset(rob, 0, sizeof(rob));
    112 for(i = 1; i <= n; i++) {
    113 scanf("%d %d %c", &rob[i].x, &rob[i].y, &rob[i].dir);
    114 //printf("%d %d %c\n", rob[i].x, rob[i].y, rob[i].dir);
    115 }
    116 flag = 0; flag2 = 1;
    117 for(i = 1; i <= m; i++) {
    118 scanf("%d %c %d", &a, &ch, &b);
    119 //printf("%d %c %d\n", a, ch, b);
    120 if(flag != 0) continue;
    121 if(ch == 'F') {
    122 flag = go(a, b);
    123 flag2 = a;
    124 } else if(ch == 'L') {
    125 b %= 4;
    126 change_L(a, b);
    127 } else {
    128 b %= 4;
    129 change_R(a, b);
    130 }
    131 }
    132 if(!flag) puts("OK");
    133 else if(flag == -1) printf("Robot %d crashes into the wall\n", flag2);
    134 else printf("Robot %d crashes into robot %d\n", flag2, flag);
    135 }
    136 return 0;
    137 }

    istringstream 的用法

    char s[N], t[N];

    cin.getline(s);
    istringstream x(s);

    while(x >> t) {
      cout << t << endl;
    }

    /*
    效果:
    比如输入
    adgg sdf dfsa sdfsd dsf
    输出则是
    adgg
    sdf
    dfsa
    sdfsd
    dsf

    也就是以空格为分割符,把字符串分割开
    */



  • 相关阅读:
    Python 标准库 BaseHTTPServer 中文翻译
    DBA Scripts
    cordova开发自己定义插件
    ASUS主板ALC887声卡,RTL81XX网卡,黑苹果驱动安装
    com.apple.installer.pagecontroller 错误 -1 pkg安装错误
    Apple 远程推送APNS 服务
    bitbucket git push 项目503失败
    在xcode5下利用Source Control 做 git 项目管理
    iOS设备定位服务开启判定
    has been modified since the precompiled header地图错误
  • 原文地址:https://www.cnblogs.com/vongang/p/2368970.html
Copyright © 2011-2022 走看看