AOJ 2320 Infinity Maze

21 篇文章 0 订阅

dfs寻找循环节


/*author: birdstorm*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

#define MAXN 105
#define N 105
#define INF 1<<30
#define eps 1.0e-10

#define For(i,m,n) for(i=(m);i<n;i++)
#define MAX(x,y) (x)>(y)?(x):(y)
#define MIN(x,y) (x)<(y)?(x):(y)

char str[MAXN][MAXN];
long long vis[MAXN][MAXN][4];
int move[4][2]={0,-1,1,0,0,1,-1,0};
int H, W, cnt;
long long L;

main()
{
    int i, j;
    int x, y, z, tx, ty;
    char *p="NESW";
    while(scanf("%d%d%lld",&H,&W,&L),H||W||L){
        memset(vis,0,sizeof(vis));
        memset(str,0,sizeof(str));
        For(i,1,H+1){
            scanf("%s",str[i]+1);
            For(j,1,W+1){
                if(str[i][j]!='.'&&str[i][j]!='#'){
                    x=j; y=i;
                    if(str[i][j]=='N') z=0;
                    if(str[i][j]=='E') z=1;
                    if(str[i][j]=='S') z=2;
                    if(str[i][j]=='W') z=3;
                    str[i][j]='.';
                }
            }
        }
        //For(i,1,H+1) {For(j,1,W+1) printf("%c",str[i][j]); puts("");}
        for(cnt=0;L;L--,cnt++){
            if(vis[y][x][z]){
                L=L%(cnt-vis[y][x][z]);
                if(!L) break;
            }
            vis[y][x][z]=cnt;
            while(1){
                tx=x+move[z][0];
                ty=y+move[z][1];
                if(str[ty][tx]=='.') break;
                z=(z+1)%4;
            }
            x=tx, y=ty;
        }
        printf("%d %d %c\n",y,x,*(p+z));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值