NOJ [1265] Flandre's Second Escape

  • 问题描述
  • Congratulation! Under your help Flandre Scarlet passed Level one. Now, she faces a hander Level. Do you want to know it? HaHa! Guess it by yourself~ = =.
    Go To Topic,this level goal is walk away from maze in shortest time. 
    In every second, she could move one block to one of the upper, lower, left and right neighboring blocks. Can the poor doggie survive? Please help him.

    Give you a map. You should finish this goal. I think you can help poor Flandre.

    <img src="    alt="        "  height="146"          width="200"         20121102012242_15560.jpg? 20121102 image res =""></img src="    alt=">

  • 输入
  • The input consists of multiple test cases. The first line of each test case contains two integers N, M ( 2 < N, M < 20 ), The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
    '*':an empty block.
    '#':a block of wall, which Flandre cannot enter; 
    'S':the entrance of maze;
    'E':the exit of maze;
    The input is terminated with two 0's. This test case is not be processed.
  • 输出
  • For each test case, print in one line "..> <..Thank you very much!!" if the Flandre can survive, or "T_T I want to go out!!" otherwise.
    包括前面几题,今天真是在刷水题啊,妈蛋,但是,为了小学妹,我还是会坚持写题解的
    这题是再水不过的DFS了
    #include<stdio.h>
    #include<string.h>
    
    int dir[4][2]={1,0,-1,0,0,-1,0,1};
    char mat[22][22];
    bool vis[22][22];
    int n,m;
    int bx,by,ex,ey;
    bool dfs(int x,int y)
    {
    	if(x==ex && y==ey)
    	  return true;
        vis[x][y]=1;
    	for(int i=0;i<4;i++)
    	{
    		int xx=x+dir[i][0];
    		int yy=y+dir[i][1];
    		if(xx<0 || yy<0 || xx>=n || yy>=m||vis[xx][yy]|| mat[xx][yy]=='#')
    		  continue;
            if(dfs(xx,yy))
              return true;
    	}
    	return false;
    }
    
    
    int main()
    {
    	while(~scanf("%d%d%*c",&n,&m) &&(n||m))
    	{
    		int i,j;
    		for(i=0;i<n;i++)
    		  scanf("%s",mat[i]);
            memset(vis,0,sizeof(vis));
            for(i=0;i<n;i++)
              for(j=0;j<m;j++)
              {
                if(mat[i][j]=='S')
    			{
    				bx=i;
    				by=j;
    			}
    			if(mat[i][j]=='E')
    			{
    				ex=i;
    				ey=j;
    			}	
              }
           if(dfs(bx,by))
              printf("..> <..Thank you very much!!\n");
           else
              printf("T_T I want to go out!!\n");
    	}
    	return 0;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值