1905-Treasure Hunt ZCMU

Description

Jill has created a new smartphone game that leads players to find a treasure. The app uses the phone's GPS to determine the player's location. The app then tells the player the direction in which to go on a route to find the treasure. When the player reaches some specific location, the app rewards the player with a (virtual) treasure.

Can you help the player determine how long it will take to find the treasure?

Input

The first line of input contains two integers R and C, each between 1 and 200, inclusive. These integers define the number of rows and columns in the playing area, respectively. The next R lines of input describe the playing area. Each line contains exactly C letters, and each letter defines the action to take in each location in the playing area. There are five possible letters: N indicates a move to the previous row, S indicates a move to the next row, W indicates a move to the previous column, E indicates a move to the next column, and T indicates the location of the treasure. Exactly one location contains the treasure.

Output

The player begins playing at the location in the first column of the first row. The player follows the directions at each location. If the player eventually reaches the treasure by following the directions, output a line containing an integer, the number of moves required to reach the treasure. If the directions cause the player to leave the playing area, output a line containing the word Out. If the directions cause the player to stay in the playing area but never reach the treasure, output a line containing the word Lost.

Sample Input

2 2

ES

TW

Sample Output

3

解析

简单的搜索

代码

#include<bits/stdc++.h>
#define INF 1e9
using namespace std;
int n,m,dis,endx,endy,flag;
char maze[205][205],book[205][205];
void dfs(int x,int y,int distance)
{
    book[x][y]++;
    if(book[x][y]>=2)
        return ;
    if(x==endx&&y==endy)
    {
        dis=distance;
        return ;
    }
    if(x<1||x>n||y<1||y>m)
     {
         flag=1;
         return ;
     }
    if(maze[x][y]=='N')
         dfs(x-1,y,distance+1);
    else if(maze[x][y]=='S')
         dfs(x+1,y,distance+1);
    else if(maze[x][y]=='W')
         dfs(x,y-1,distance+1);
    else if(maze[x][y]=='E')
         dfs(x,y+1,distance+1);
    return ;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
      for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
          cin>>maze[i][j];
          if(maze[i][j]=='T')
          {
            endx=i; endy=j;
          }
        }
      memset(book,0,sizeof(book));
      dis=INF; flag=0;
      dfs(1,1,0);
      if(flag)
        printf("Out\n");
      else if(dis!=INF)
        printf("%d\n",dis);
      else
        printf("Lost\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值