P1518 [USACO2.4] 两只塔姆沃斯牛 The Tamworth Two
题目描述
两只牛逃跑到了森林里。Farmer John 开始用他的专家技术追捕这两头牛。你的任务是模拟他们的行为(牛和 John)。
追击在 10×1010×10 的平面网格内进行。一个格子可以是:一个障碍物,两头牛(它们总在一起),或者 Farmer John。两头牛和 Farmer John 可以在同一个格子内(当他们相遇时),但是他们都不能进入有障碍的格子。
一个格子可以是:
.空地;*障碍物;C两头牛;FFarmer John。
这里有一个地图的例子:
*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......
牛在地图里以固定的方式游荡。每分钟,它们可以向前移动或是转弯。如果前方无障碍(地图边沿也是障碍),它们会按照原来的方向前进一步。否则它们会用这一分钟顺时针转 90 度。 同时,它们不会离开地图。
Farmer John 深知牛的移动方法,他也这么移动。
每次(每分钟)Farmer John 和两头牛的移动是同时的。如果他们在移动的时候穿过对方,但是没有在同一格相遇,我们不认为他们相遇了。当他们在某分钟末在某格子相遇,那么追捕结束。
读入十行表示地图。每行都只包含 10 个字符,表示的含义和上面所说的相同。保证地图中只有一个 F 和一个 C。F 和 C 一开始不会处于同一个格子中。
计算 Farmer John 需要多少分钟来抓住他的牛,假设牛和 Farmer John 一开始的行动方向都是正北(即上)。 如果 John 和牛永远不会相遇,输出 0。
输入格式
输入共十行,每行 10 个字符,表示如上文描述的地图。
输出格式
输出一个数字,表示 John 需要多少时间才能抓住牛们。如果 John 无法抓住牛,则输出 0。
我真的是无语了,就因为这个破输入我改了半天才AC。不知道它每一行末尾都是用的啥符号。。。
此题收获:用了一个表示状态的bool数组存储特殊值是否经过来判断会否陷入循环而永不相遇。
AC代码:
#include<iostream> #include<cstdio> using namespace std; int dis[4][2] = { -1,0,0,1,1,0,0,-1 }; bool check[1000000] = { false }; int x, y, tx, ty, cnt = 0, temp1 = 0, temp2 = 0; int main() { int num[12][12] = {}; char s[12] = {}; char c; for (int i = 1; i <= 10; i++) { /*cin.getline(s, 12);c = s[i1 - 1];*/ for (int i1 = 1; i1 <= 10; i1++) { c = getchar(); while((c != '*') && (c != '.') && (c != 'C') && (c != 'F')) c = getchar(); if (c == '*') num[i][i1] = 0; else if (c == '.') num[i][i1] = 1; else if (c == 'F') { num[i][i1] = 1; tx = i; ty = i1; } else { num[i][i1] = 1; x = i; y = i1; } } } while (1) { if ((tx == x) && (ty == y)) break; cnt++; if (num[tx + dis[temp1][0]][ty + dis[temp1][1]] ==1) { tx = tx + dis[temp1][0]; ty = ty + dis[temp1][1]; } else { temp1++; if (temp1 == 4) temp1 = 0; } if (num[x + dis[temp2][0]][y + dis[temp2][1]] == 1) { x = x + dis[temp2][0]; y = y + dis[temp2][1]; } else { temp2++; if (temp2 == 4) temp2 = 0; } if(check[x + 10 * tx + 100 * y + 1000 * ty + 10000 * temp1 + 100000 * temp2] == true) { cout << 0; return 0; } else check[x + 10 * tx + 100 * y + 1000 * ty + 10000 * temp1 + 100000 * temp2] = true; } cout << cnt; }

浙公网安备 33010602011771号