杭电 1242 Rescue 解题报告

    题意差不多就是天使的朋友去就天使。刚开始从r开始遍历,步数依次+1,遇x则+2,但是一直报错。

    然后笔者就一直在网上找呀。。。一般都是bfs,优先队列,栈啥的。代码看起来就很复杂,笔者也懒得看了。但是从a到r的逆向思想却还是有用的。

    改代码,只用数组,AC了。如下:

#include <iostream>
using namespace std;

int s[202][202];

int main()
{
    int i,j,t,n,m,flag;
    char str[202];
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
            for(cin>>(str+1),j=1;j<=m;j++)
                if(str[j]=='a')
                    s[i][j]=1;
                else if(str[j]=='#')
                    s[i][j]=-1;
                else if(str[j]=='x')
                    s[i][j]=-2;
                else if(str[j]=='r')
                    s[i][j]=-3;
                else
                    s[i][j]=0;
        t=1;
        while(1)
        {
            flag=1;
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    if(s[i][j]==t)
                    {
                        flag=0;
                        if(s[i+1][j]==0)
                            s[i+1][j]=t+1;
                        else if(s[i+1][j]==-2)
                            s[i+1][j]=t+2;

                        if(s[i-1][j]==0)
                            s[i-1][j]=t+1;
                        else if(s[i-1][j]==-2)
                            s[i-1][j]=t+2;

                        if(s[i][j+1]==0)
                            s[i][j+1]=t+1;
                        else if(s[i][j+1]==-2)
                            s[i][j+1]=t+2;

                        if(s[i][j-1]==0)
                            s[i][j-1]=t+1;
                        else if(s[i][j-1]==-2)
                            s[i][j-1]=t+2;

                        if(s[i-1][j]==-3||s[i][j-1]==-3||s[i+1][j]==-3||s[i][j+1]==-3)
                        {
                            flag=2;
                            i=n+1;
                            j=m+1;
                        }
                    }
                    else if(s[i][j]==t+1)
                        flag=0;

            if(flag)
                break;
            t++;
        }
        if(flag==2)
            cout<<t<<endl;
        else
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
}

    看到有代码长度是801B的,顺便精简一下吧,如下,测试AC:

#include <iostream>
using namespace std;

int main()
{
    int i,j,t,n,m,flag,a;
    int s[202][202];
    char str[202];
    while(cin>>n>>m)
    {
        for(t=i=1;i<=n;i++)
            for(cin>>(str+1),j=1;j<=m;j++)
                if(str[j]=='a')
                    s[i][j]=1;
                else if(str[j]=='#')
                    s[i][j]=-1;
                else if(str[j]=='x')
                    s[i][j]=-2;
                else if(str[j]=='r')
                    s[i][j]=-3;
                else
                    s[i][j]=0;
        while(1)
        {
            for(flag=i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    if(s[i][j]==t)
                    {
                        for(flag=0,a=-2;a<=2;a++)
                            if(s[i+a/2][j+a%2]==0)
                                s[i+a/2][j+a%2]=t+1;
                            else if(s[i+a/2][j+a%2]==-2)
                                s[i+a/2][j+a%2]=t+2;
                            else if(s[i+a/2][j+a%2]==-3)
                            {
                                flag=2;
                                i=n+1;
                                j=m+1;
                            }
                    }
                    else if(s[i][j]==t+1)
                        flag=0;
            if(flag) break;
            t++;
        }
        if(flag==2)
            cout<<t<<endl;
        else
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
}

 

posted @ 2013-02-18 15:17  SF-_-  阅读(380)  评论(0编辑  收藏  举报