“师创杯”精英赛G---限额逃生2(BFS)---已AC+错误分析

24 篇文章 0 订阅

Problem G: G-险恶逃生II

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 27   Solved: 5
[ Submit][ Status][ Web Board]

Description

    SOS!!!koha is trapped in the dangerous maze.He need your help again.
    The maze is a 2D grid consisting of n rows and m columns. Each cell in the maze may have a stone or may
be occupied by zero or more monsters. The cell that has a stone Koha(including the monsters) cannot enter. Only one of the cells is designated as the exit cell.
    Kona,including the monsters may move in the maze.In a single move,each of them may perform one of the Following actions:
    1. do nothing
    2. move from the current cell to one of the four adjacent cells.
    3. if Koha is located on the exit cell, he may leave the maze. Only he can perform this move — all Monsters will never leave the maze by using this type of movement.
    After each time Kona makes a single move,each of the monsters simultaneously make a single move ,however the choice of which move to make may be different for each of the monsters.
 
    If koha and x(x>0) monsters are located on the same cell, exactly x monsters will ensue that time(since he will be battling each of those x breeders once).After the battle, all of those x monsters will be killed.
    Now,Koha would like to leave the maze,however the monsters will all know his exact sequence of moves even before he makes his first move.All of them will move in such way that will guarentee a monster battle with Koha,if possible. The monsters that couldn't battle him will do nothing.
 
    All right.Your task is to print the mininum number of monster battles that kona must participates in,note that you are not required to minimize the number of moves kona makes.

Input

For each case,the first line consists of two integes: n and m(1<=n,m<=1000),denoting the number of rows and the number of columns in the maze.the next n rows will echo depict a row of the map,where each character represents the content of a single cell:
'T':A cell occupied by a stone.
'S':An empty cell,and Kona's starting position.
'E':An empty cell,and where the exit is located.
A digit(0-9): A cell represented by a digit x means that the cell is empty and is occupied by x monsters(if x is zero,it means the cell is not occupied by any monsters).
 
It is guaranteed that it will be possible for Kona to leave the maze.

Output

For each case,ouput a single line denoted the mininum possible number of monster battles that koha has to participate in if you pick a strategy that minimize this number.

Sample Input

5 7
000E0T3
T0TT0T0
010T0T0
2T0T0T0
0T0S000
1 4
SE23

Sample Output

3
2

HINT

For the second case,kona and the monsters located in (0,2) will reach

the exit cell simultaneously ,so he must kill them.








AC代码:

#include <iostream>
#include <cstring>
#define size 1000

using namespace std;
char map[1011][1011];
int res[1011][1011];
int x1,x2,y1,y2,n,m;
struct point
{
    int x,y;
} r[size*size+11];
int dis[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
void dfs()
{
    int i,tail,head,xx,yy,x11,y11;
    memset(res,-1,sizeof(res));
    res[x1][y1]=0;
    r[0].x=x1;
    r[0].y=y1;
    tail=1;
    head=0;
    while(tail != head)
    {
        x11=r[head].x;
        y11=r[head].y;
        for(i=0; i<4; i++)
        {
            xx=x11+dis[i][0];
            yy=y11+dis[i][1];
            if(map[xx][yy]!='T'&&res[xx][yy]==-1)
            {
                r[tail].x=xx;
                r[tail].y=yy;
                res[xx][yy] = 1 + res[x11][y11];
                tail++;
            }
        }
        head++;
    }

}
int main()
{
    int i,j;
    int num,max=0;
    while(cin>>m>>n)
    {
        for(i=0; i<=n+1; i++)
            map[0][i]=map[m+1][i]='T';
        for(i=0; i<=m+1; i++)
            map[i][0]=map[i][n+1]='T';
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='E')
                {
                    x1=i;
                    y1=j;
                }
                if(map[i][j]=='S')
                {
                    x2=i;
                    y2=j;
                }
            }
        dfs();
        num=res[x2][y2];
        max=0;
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
                if(map[i][j]>'0'&&map[i][j]<='9')
                    if(res[i][j]<=num&&res[i][j]!=-1)
                    max+=map[i][j]-'0';
        cout<<max<<endl;
    }
    return 0;
}



针对4楼前博文代码未AC原因分析:

   第一次错误提示 :

                                正确输出:0,我的输出:36


                                 分析,从错误原因来看,应当是没有考虑到特殊情况造成的,仔细考虑后发现,由于在广度搜索中用 -1打表,所以在怪兽找不到出口时,当前的表格数据为-1.然而却符合了if(map[i][j]>'0'&&map[i][j]<='9'&& res[i][j]<=num)的判断,于是在判断中多加了个&&res[i][j]!=-1.   特殊情况解决,进行特殊情况测试数据正确,于是将代码提交。但是!!!!更加腚疼的问题又来了~~~ ---runtime error!!!,估计是访问越界数组开小了,在周童辉大神的帮助分析下 发现 果真 点坐标开小了,于是果断把r[1011]开到了 r[1011*1011],成功AC~~~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值