思路
一道不算特别大的模拟,思维量小,简单
唯一的易错点在于,前进的方向,即向北到底是怎么移动的?我一开始写的是 dr 数组的第一个是这样的 {0,1} 完全就是把二维数组当数学中的坐标系来写了
然而其实在二维数组里,竖着的是 x 轴,且是向下时增大,横着的是 y 轴,并且是向右时增大
所以正确的 dr 数组应该是
1
| int dr[][2] = {{-1,0},{0,1},{1,0},{0,-1}};
|
难度:下位黄
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std;
signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); vector<vector<char> > a(10,vector<char>(10)); int x1,y1,x2,y2; for(int i = 0 ; i < 10 ; i++){ string line; cin >> line; for(int j = 0 ; j < 10 ;j++){ a[i][j] = line[j]; if(a[i][j] == 'F'){ x1 = i, y1 = j; a[i][j] = '.'; } if(a[i][j] == 'C'){ x2 = i, y2 = j; a[i][j] = '.'; } } } int dr[][2] = {{-1,0},{0,1},{1,0},{0,-1}}; int d1 = 0 ,d2 = 0; int t = 0; bool vis[10][10][4][10][10][4] = {}; while(x1 != x2 || y1 != y2){ if (vis[x1][y1][d1 % 4][x2][y2][d2 % 4]){ cout << 0 << endl; return 0; } vis[x1][y1][d1 % 4][x2][y2][d2 % 4] = true; int dx1 = x1 + dr[d1 % 4][0]; int dx2 = x2 + dr[d2 % 4][0]; int dy1 = y1 + dr[d1 % 4][1]; int dy2 = y2 + dr[d2 % 4][1]; if (dx1 < 0 || dx1 > 9 || dy1 < 0 || dy1 > 9 || a [dx1][dy1] == '*'){ d1++; } else{ x1 = dx1, y1 = dy1; } if (dx2 < 0 || dx2 > 9 || dy2 < 0 || dy2 > 9 || a [dx2][dy2] == '*'){ d2++; } else{ x2 = dx2, y2 = dy2; } t++; } cout << t << endl; return 0; }
|
顺便一提
将代码中的 vis 的记忆化数组换成下面的过剩判断也能通过本题
1 2 3 4
| if(t >= 10000000){ cout << 0 << endl; return 0; }
|