标签:return int Phillip bfs && Trains maze false 移动
Phillip and Trains
思路
读了一个假题,一开始我以为有四种走法,上,下,不动,右。然后直接wa我3次,,,,
题意是,人物先向右移动一格,然后可以选择向前,向后,或者不动,人物移动完之后,火车会向左移动两格。
可以理解为,人物先向右移动一格,然后选择向前,向后或者不动,进而再向右移动两格,在任意时刻都要保证人物不能与火车相撞。
接着就是简单的bfs了, 判断是否可以走当前一步时要注意,一定要判断其路径上的每一个点都是可以移动的。
代码
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int, int> PII;
const int ax[3] = {-1, 0, 1};
const int ay[4] = {1, 1, 1};
const int N = 110;
int maze[3][N], n, k;
bool judge(int x, int y, int k) {
if(k == 0 && (maze[x][y + 1] || maze[x - 1][y + 1])) return false;
if(k == 1 && (maze[x][y + 1])) return false;
if(k == 2 && (maze[x][y + 1] || maze[x + 1][y + 1])) return false;
return true;
}
bool bfs(int x, int y) {
maze[x][y] = 1;
queue<PII> q;
q.push(mp(x, y));
while(!q.empty()) {
PII temp = q.front();
// cout << temp.first << " " << temp.second << endl;
q.pop();
if(temp.second >= n - 1) return true;
for(int i = 0; i < 3; i++) {
int tempx = temp.first + ax[i];
int tempy = temp.second + ay[i];
if(tempx >= 3 || tempx < 0) continue;
// cout << i << endl;
if(judge(temp.first, temp.second, i)) {
if(maze[tempx][tempy + 1] || maze[tempx][tempy + 2]) continue;
tempy += 2;
// cout << i << endl;
q.push(mp(tempx, tempy));
maze[tempx][tempy] = 1;
}
}
}
return false;
}
int main() {
// freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
memset(maze, 0, sizeof maze);
scanf("%d %d", &n, &k);
int x, y;
char c;
for(int i = 0; i < 3; i++) {
getchar();
for(int j = 0; j < n; j++) {
scanf("%c", &c);
if(c >= 'A' && c <= 'Z')
maze[i][j] = 1;
if(c == 's')
x = i, y = j;
}
}
// for(int i = 0; i < 3; i++)
// for(int j = 0; j < n; j++)
// printf("%d%c", maze[i][j], j + 1 == n ? '\n' : ' ');
if(bfs(x, y)) puts("YES");
else puts("NO");
}
return 0;
}
标签:return,int,Phillip,bfs,&&,Trains,maze,false,移动 来源: https://www.cnblogs.com/lifehappiness/p/12844852.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。