列舉 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, √
|
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
也就是以空格為分割符,把字串分割開*/