洛谷 P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two

模拟题,需要注意的是如何判断进入死循环

#include<iostream>
#include<cstdio>
#define MAXN 15
using namespace std;
struct Node{
    int x,y,d;
};
Node f,c;
int res;
bool vis[100000];
char a[MAXN][MAXN];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
void move(Node& z){
    int nx=z.x+dx[z.d];
    int ny=z.y+dy[z.d];
    if(nx<1||ny<1||nx>10||ny>10||a[nx][ny]=='*'){
        z.d=(z.d+1)%4;
        return ;
    }
    z.x=nx;z.y=ny;
}
int main(){
#ifdef WINE
    freopen("data.in","r",stdin);
#endif
    for(int i=1;i<=10;i++)
        for(int j=1;j<=10;j++){
            scanf(" %c ",&a[i][j]);
            if(a[i][j]=='F'){
                a[i][j]='.';
                f.x=i;f.y=j;f.d=0;
            }
            if(a[i][j]=='C'){
                a[i][j]='.';
                c.x=i;c.y=j;c.d=0;
            }
        }
    while(true){
        if(f.x==c.x&&f.y==c.y)break;
        int k=f.x+f.y*10+c.x*100+c.y*1000+f.d*4000+c.d*16000;
        if(vis[k]){
            printf("0");
            return 0;
        }
        vis[k]=true;
        move(f);move(c);
        res++;
    }
    printf("%d",res);
    return 0;
}

posted @ 2020-07-26 12:17  winechord  阅读(112)  评论(0编辑  收藏  举报