校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛1002

Problem B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 254    Accepted Submission(s): 11


Problem Description
As you know, mazes in some role-playing games are quite complicated. Usually, it should take a long time to keep trying different ways to get out of a maze. But here comes a question, that with the help of algorithm and computer, can we figure out a faster way to solve a maze?

So we are taking a experiment here. Now we have a maze in a shape of an N times M lattice (N rows and M columns). In the lattice, some grids are empty so that we may stand on them, while others are obstacles and it's forbidden to locate on these grids. In each move, we can make a move to the 8 adjacent empty grids with an exception: we cannot move through two obstacles' gap nor outside the maze. The following figure shows the moving rules.


Figure 1: Moving rules, in which white for empty grids and grey for obstacles


In order to get out of it, we need to trigger activations in the maze. Totally there are  K  activations, each located on a specific empty grid. If we are standing on a grid with an activation, it will be triggered (and it disappears immediately). In the maze, our target is to trigger all  K  activations in order. When the last activation is triggered, we may leave it at once. 

Now we have already known the positions of obstacles and activations. Initially, we are standing on a specific grid in the maze. Can you figure out how many moves we should make at least to get out of it?
 

Input
The number of test cases  T(T20)  will occur in the first line of input.

For each test case:

The first line contains the number of rows  N(2N100) , columns  M(2M100)  and activations  K(1K10) .

Each of the following  N  lines contains  M  characters, where a '#' denotes an obstacle and a '.' denotes an empty grid.

The following line contains the start position  (x,y) , which denotes the grid on the  x th row and the  y th column. It's always an empty grid.

Each of the following  K  lines describe a coordinate of the activations. All activations will not occur on obstacles, and two activations will not share the same grid. We need to trigger all of them in order of the given list.
 

Output
For each test case, output the minimum moves we need to make to reach the target. If we cannot get out of the maze, output -1 instead.
 

Sample Input
  
  
3 3 3 2 ... ... ... 1 1 1 3 2 2 3 3 1 ... .#. ... 1 1 3 3 2 3 1 ..# .#. 1 1 2 3
 
Sample Output
  
  
3 3 -1
WA了一万年,这题卡住之后一直没心思做接下来的题目了 
被卡地方
     1.必须要按顺序经过所有点,也就是说不能提前经过有标记的点,必须按顺序
     2.要经过的点可能为起点
吃一堑长一智,以后要看清楚题目
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
    int x,y,d;
}s[1005],u;
int dir[9][2],T,i,n,m,k,flag,flag1,ans,j;
char g[105][105];
int vis[105][105];
int ok(node t,int ty)
{
    t.x--;t.y--;
    if(t.x<0||t.x>=n||t.y<0||t.y>=m)
        return 0;
    if(g[t.x][t.y]=='#')
        return 0;
    if(ty==1&&g[t.x+1][t.y]=='#'&&g[t.x][t.y+1]=='#')
        return 0;
    if(ty==3&&g[t.x][t.y-1]=='#'&&g[t.x+1][t.y]=='#')
        return 0;
    if(ty==6&&g[t.x-1][t.y]=='#'&&g[t.x][t.y+1]=='#')
        return 0;
    if(ty==8&&g[t.x-1][t.y]=='#'&&g[t.x][t.y-1]=='#')
        return 0;
    return 1;
}
int main()
{
    dir[1][0]=-1;
    dir[1][1]=-1;
    dir[2][0]=-1;
    dir[2][1]=0;
    dir[3][0]=-1;
    dir[3][1]=1;
    dir[4][0]=0;
    dir[4][1]=-1;
    dir[5][0]=0;
    dir[5][1]=1;
    dir[6][0]=1;
    dir[6][1]=-1;
    dir[7][0]=1;
    dir[7][1]=0;
    dir[8][0]=1;
    dir[8][1]=1;node t;
    scanf("%d",&T);
    while(T--)
    {
        queue<node>q;
        while(!q.empty())
            q.pop();
        scanf("%d%d%d",&n,&m,&k);
        for(i=0;i<n;i++)
            scanf("%s",g[i]);
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&s[0].x,&s[0].y);
        for(i=1;i<=k;i++)
        {
            scanf("%d%d",&s[i].x,&s[i].y);
        }
        flag=1;
        ans=0;
        for(j=0;j<k;j++)
        {
            memset(vis,0,sizeof(vis));
            vis[s[j].x][s[j].y]=1;
            for(i=j+2;i<=k;i++)
                vis[s[i].x][s[i].y]=1;
            if(flag)
            {
                if(s[0].x==s[j+1].x&&s[0].y==s[j+1].y)
                {
                    if(j!=0){flag=0;break;}
                    else continue;
                }
                u.x=s[j].x;u.y=s[j].y;u.d=0;
                q.push(u);
                flag1=1;
                while(!q.empty())
                {
                    u=q.front();
                    q.pop();
                    for(i=1;i<=8;i++)
                    {
                        t.x=u.x+dir[i][0];
                        t.y=u.y+dir[i][1];
                        t.d=u.d+1;
                        if(ok(t,i)&&!vis[t.x][t.y])
                        {
                            q.push(t);
                            vis[t.x][t.y]=1;
                            if(t.x==s[j+1].x&&t.y==s[j+1].y)
                            {
                                flag1=0;
                                ans+=t.d;
                                while(!q.empty())
                                    q.pop();
                                break;
                            }
                        }
                    }
                }
                if(flag1) flag=0;
            }
        }
        if(flag) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值