美文网首页
P1518 [USACO2.4] 两只塔姆沃斯牛 The Tam

P1518 [USACO2.4] 两只塔姆沃斯牛 The Tam

作者: louyang | 来源:发表于2024-10-10 06:11 被阅读0次

模拟算法:

#include<iostream>
using namespace std;

char land[11][11];
int dx[4] = {-1, 0, 1,  0};
int dy[4] = { 0, 1, 0, -1};
int cx, cy, fx, fy, fdir, cdir, ans;

bool check(int x, int y, int dir) {
  x = x + dx[dir], y = y + dy[dir];
  return x >= 1 && x <= 10 && y >= 1 && y <= 10 && land[x][y] != '*';
}

int main() {
  for(int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
      cin >> land[i][j];
      if (land[i][j] == 'F') {
        fx = i, fy = j;
      }
      if(land[i][j] == 'C') {
        cx = i, cy = j;
      }
    }
  }

  for (int i = 1; i <= 1000000; i++) {
    if (fx == cx && fy == cy) {
      cout << ans << endl;
      return 0;
    }
    
    if (check(fx,fy,fdir)) {
      fx = fx + dx[fdir];
      fy = fy + dy[fdir];
    } else {
      fdir = (fdir+1) % 4;
    }
    if (check(cx,cy,cdir)) {
      cx = cx + dx[cdir];
      cy = cy + dy[cdir];
    } else {
      cdir = (cdir+1) % 4;
    }
    ans++;
  }
  cout << 0 << endl;
  return 0;
}

相关文章

网友评论

      本文标题:P1518 [USACO2.4] 两只塔姆沃斯牛 The Tam

      本文链接:https://www.haomeiwen.com/subject/xxcwrjtx.html