I’m stuck!题解
作者:互联网
题面见链接:https://www.cnblogs.com/hyf200803050946/p/14882604.html
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node{
int x,y;
}t1,t2;
int r,c,ans;
char a[55][55];
int sx,sy,tx,ty;
bool flag1[55][55],flag2[55][55];
bool L[55][55][55][55]; //L[x][y][i][j]表示从(x,y)到(i,j)的状态
queue<node>q;
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
bool check(int x,int y){return x>=1&&x<=r&&y>=1&&y<=c&&a[x][y]!='#';}
int main(){
scanf("%d %d",&r,&c);
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
scanf("\n%c",&a[i][j]);
if(a[i][j]=='S')sx=i,sy=j;
if(a[i][j]=='T')tx=i,ty=j;
}
}
t1.x=sx,t1.y=sy;
q.push(t1);
flag1[t1.x][t1.y]=true;
while(!q.empty()){
t1=q.front();
q.pop();
int I=0,J=0;
if(a[t1.x][t1.y]=='+'||a[t1.x][t1.y]=='S'||a[t1.x][t1.y]=='T')I=0,J=3;
if(a[t1.x][t1.y]=='-')I=2,J=3;
if(a[t1.x][t1.y]=='|')I=0,J=1;
if(a[t1.x][t1.y]=='#')continue;
for(int i=I;i<=J;i++){
t2.x=t1.x+dir[i][0],t2.y=t1.y+dir[i][1];
if(check(t2.x,t2.y)){
L[t1.x][t1.y][t2.x][t2.y]=true;
if(!flag1[t2.x][t2.y]){
flag1[t2.x][t2.y]=true;
q.push(t2);
}
}
}
}
if(!flag1[tx][ty]){
printf("I'm stuck!");
return 0;
}
while(!q.empty())q.pop();
t1.x=tx,t1.y=ty;
flag2[t1.x][t1.y]=true;
q.push(t1);
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=0;i<=3;i++){
t2.x=t1.x+dir[i][0],t2.y=t1.y+dir[i][1];
if(check(t2.x,t2.y)&&L[t2.x][t2.y][t1.x][t1.y]&&!flag2[t2.x][t2.y]){
flag2[t2.x][t2.y]=true;
q.push(t2);
}
}
}
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(flag1[i][j]&&!flag2[i][j])ans++;
}
}
printf("%d",ans);
return 0;
}
格式化后……
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node {
int x, y;
} t1, t2;
int r, c, ans;
char a[55][55];
int sx, sy, tx, ty;
bool flag1[55][55], flag2[55][55];
bool L[55][55][55][55]; // L[x][y][i][j]表示从(x,y)到(i,j)的状态
queue<node> q;
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } };
bool check(int x, int y) { return x >= 1 && x <= r && y >= 1 && y <= c && a[x][y] != '#'; }
int main() {
scanf("%d %d", &r, &c);
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
scanf("\n%c", &a[i][j]);
if (a[i][j] == 'S')
sx = i, sy = j;
if (a[i][j] == 'T')
tx = i, ty = j;
}
}
t1.x = sx, t1.y = sy;
q.push(t1);
flag1[t1.x][t1.y] = true;
while (!q.empty()) {
t1 = q.front();
q.pop();
int I = 0, J = 0;
if (a[t1.x][t1.y] == '+' || a[t1.x][t1.y] == 'S' || a[t1.x][t1.y] == 'T')
I = 0, J = 3;
if (a[t1.x][t1.y] == '-')
I = 2, J = 3;
if (a[t1.x][t1.y] == '|')
I = 0, J = 1;
if (a[t1.x][t1.y] == '#')
continue;
for (int i = I; i <= J; i++) {
t2.x = t1.x + dir[i][0], t2.y = t1.y + dir[i][1];
if (check(t2.x, t2.y)) {
L[t1.x][t1.y][t2.x][t2.y] = true;
if (!flag1[t2.x][t2.y]) {
flag1[t2.x][t2.y] = true;
q.push(t2);
}
}
}
}
if (!flag1[tx][ty]) {
printf("I'm stuck!");
return 0;
}
while (!q.empty()) q.pop();
t1.x = tx, t1.y = ty;
flag2[t1.x][t1.y] = true;
q.push(t1);
while (!q.empty()) {
t1 = q.front();
q.pop();
for (int i = 0; i <= 3; i++) {
t2.x = t1.x + dir[i][0], t2.y = t1.y + dir[i][1];
if (check(t2.x, t2.y) && L[t2.x][t2.y][t1.x][t1.y] && !flag2[t2.x][t2.y]) {
flag2[t2.x][t2.y] = true;
q.push(t2);
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (flag1[i][j] && !flag2[i][j])
ans++;
}
}
printf("%d", ans);
return 0;
}
标签:flag1,stuck,55,题解,int,bool,&&,include 来源: https://www.cnblogs.com/hyf200803050946/p/14882614.html