2018年东北农业大学春季校赛-wyh的吃鸡

时间:2022-09-14 20:45:57

BFS:

1. 从起点开始BFS,遇到X点则return;

2. vis[px][py][0]代表经过pxpy这点前还没有找到车;

 vis[px][py][1]代表经过pxpy这点前已经找到车;

3. ip记录是否找到车;

 d表示方向

4. 最后判断时间是否超时;

5. 简单的BFS,结束!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<cmath>
#define test printf("***\n")
#define ka getchar();getchar()
#define ka1 getchar()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int N = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
struct lp {
int x, y,d,ip,step;
friend bool operator <(const lp &a,const lp &b){
if(a.step!=b.step)return a.step>b.step;
return a.ip<b.ip;
}
} now, t;
int n, k;
char ar[N][N];
int dir[][] = {, , -, , , , , -};
bool vis[N][N][];
int bfs(int a, int b)
{
priority_queue<lp>Q;
memset(vis,,sizeof(vis));
t.x = a; t.y = b;
t.d = -;t.ip=;
t.step=;
Q.push(t);
vis[a][b][]=;
while(!Q.empty()) {
t = Q.top(); Q.pop();
for(int i = ; i < ; ++i) {
int px = t.x + dir[i][], py = dir[i][] + t.y;
if(px < || py < || px >= n || py >= n)continue;
if(ar[px][py] == 'O')continue;
if(t.step>k)return ;
if(t.ip == ) {
if(vis[px][py][])continue;
if(t.d != - && t.d != i)continue;
if(t.d != -) {
now.d = -;now.ip=;
now.x = px; now.y = py;
vis[px][py][]=;
now.step = t.step + ;
if(ar[px][py]=='X')return now.step;
if(ar[px][py]=='C'){
now.ip=;
vis[px][py][]=;
}
Q.push(now);
} else {
now.d = i;now.ip=;
now.x = t.x; now.y = t.y;
now.step = t.step + ;
Q.push(now);
}
}else{
if(vis[px][py][])continue;
now.d = i;now.ip=;
now.x = px; now.y = py;
now.step = t.step + ;
if(ar[px][py]=='X')return now.step;
vis[px][py][]=;
Q.push(now);
}
}
}
return ;
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int a, b;
scanf("%d%d", &n, &k);
for(int i = ; i < n; ++i) {
scanf("%s", &ar[i]);
for(int j = ; j < n; ++j) {
if(ar[i][j] == 'S')a = i, b = j;
}
}
int ans = bfs(a, b);
if(ans!=&&ans<=k) {
printf("YES\n%d\n", ans);
} else {
printf("NO\n");
}
}
return ;
}
/*
3
2 3
.X
S.
2 3
.X
SC
2 4
.X
S.
*/

题目:

https://www.nowcoder.com/acm/contest/93/H