【USACO2.4.1】兩隻塔姆沃斯牛 模擬
阿新 • • 發佈:2019-02-15
純粹的按照題目意思的模擬,
小技巧:
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1}; 定義方向變動陣列。 改變方向的話,只要+1 再MOD 4即可。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; char map[12][12]; int fx,fy,cx,cy; int df=0, dc=0; void init() { for (int i = 0; i != 12; ++ i) for (int j = 0; j != 12; ++ j) map[i][j] = '*'; for (int i = 1; i <= 10; ++ i) { scanf("%s", map[i] + 1) ; map[i][strlen(map[i] + 1) + 1] = '*'; } for (int i = 1; i <= 10; ++ i) for (int j = 1; j <= 10; ++ j) { if (map[i][j] == 'C') { cx = i; cy = j; } if (map[i][j] == 'F') { fx = i; fy = j; } } } const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; void doit() { int t = 0; while (++t <= 1000) { int nfx = fx + dx[df]; int nfy = fy + dy[df]; int ncx = cx + dx[dc]; int ncy = cy + dy[dc]; if (map[nfx][nfy] == '*') { df = (df + 1) % 4; nfx = fx; nfy = fy; } if (map[ncx][ncy] == '*') { dc = (dc + 1) % 4; ncx = cx; ncy = cy; } if (cx == fx && cy == fy) { cout << t - 1 << endl; return; } fx = nfx, fy = nfy, cx = ncx, cy = ncy; } cout<< 0 << endl; } int main() { init(); doit(); return 0; }