2014年2月22日 星期六

[UVA] 532 - Dungeon Master

/*20140223 hanting*/
#include <iostream>
using namespace std;
int main()
{
    int I,J,K;
    char c[100][100][100];
    while(cin>>I>>J>>K && (I!=0 || J!=0 || K!=0))
    {
        int S_I,S_J,S_K;
        int E_I,E_J,E_K;
        for(int i=0;i<I;i++)
            for(int j=0;j<J;j++)
                for(int k=0;k<K;k++)
                {
                    cin>>c[i][j][k];
                    if(c[i][j][k]=='S')
                    {
                        S_I=i; S_J=j; S_K=k;
                    }
                    if(c[i][j][k]=='E')
                    {
                        E_I=i; E_J=j; E_K=k;
                        c[i][j][k]='.';
                    }
                }
        int a[27000][3];
        int b[27000][3]={S_I,S_J,S_K};
        int count = 0;
        int n=1;
        int b_num=1;
        while(n)
        {//cout<<"+"<<endl;;
            n=0;
            int a_num=b_num;
            for(int i=0;i<b_num;i++)
                for(int j=0;j<3;j++)
                    a[i][j]=b[i][j];
            b_num=0;
            for(int x=0;x<a_num;x++)
            {//cout<<"a_num="<<a_num<<"bnum="<<b_num<<endl;;
                if(c[ a[x][0] ][ a[x][1] ][ a[x][2]+1 ]=='.')
                {//cout<<"test1"<<endl;
                    b[b_num][0]=a[x][0];
                    b[b_num][1]=a[x][1];
                    b[b_num][2]=a[x][2]+1;
                    b_num++;
                    n=1;
                    c[ a[x][0] ][ a[x][1] ][ a[x][2]+1 ]='x';
                }
                if(c[ a[x][0] ][ a[x][1] ][ a[x][2]-1 ]=='.')
                {//cout<<"test2"<<endl;
                    b[b_num][0]=a[x][0];
                    b[b_num][1]=a[x][1];
                    b[b_num][2]=a[x][2]-1;
                    b_num++;
                    n=1;
                    c[ a[x][0] ][ a[x][1] ][ a[x][2]-1 ]='x';
                }
                if(c[ a[x][0] ][ a[x][1]+1 ][ a[x][2] ]=='.')
                {//cout<<"test3"<<endl;
                    b[b_num][0]=a[x][0];
                    b[b_num][1]=a[x][1]+1;
                    b[b_num][2]=a[x][2];
                    b_num++;
                    n=1;
                    c[ a[x][0] ][ a[x][1]+1 ][ a[x][2] ]='x';
                }
                if(c[ a[x][0] ][ a[x][1]-1 ][ a[x][2] ]=='.')
                {//cout<<"test4"<<endl;
                    b[b_num][0]=a[x][0];
                    b[b_num][1]=a[x][1]-1;
                    b[b_num][2]=a[x][2];
                    b_num++;
                    n=1;
                    c[ a[x][0] ][ a[x][1]-1 ][ a[x][2] ]='x';
                }
                if(c[ a[x][0]+1 ][ a[x][1] ][ a[x][2] ]=='.')
                {//cout<<"test5"<<endl;
                    b[b_num][0]=a[x][0]+1;
                    b[b_num][1]=a[x][1];
                    b[b_num][2]=a[x][2];
                    b_num++;
                    n=1;
                    c[ a[x][0]+1 ][ a[x][1] ][ a[x][2] ]='x';
                }
                if(c[ a[x][0]-1 ][ a[x][1] ][ a[x][2] ]=='.')
                {//cout<<"test6"<<endl;
                    b[b_num][0]=a[x][0]-1;
                    b[b_num][1]=a[x][1];
                    b[b_num][2]=a[x][2];
                    b_num++;
                    n=1;
                    c[ a[x][0]-1 ][ a[x][1] ][ a[x][2] ]='x';
                }
            }
    /*int xxx;
    cin>>xxx;
    for(int b=0 ; b<3 ; b++)
    {
        for(int n=0 ; n<4 ; n++)
        {
            for(int m=0 ; m<5 ; m++)
            {
                cout<<c[b][n][m];
            }cout<<endl;
        }cout<<endl;
    }cout<<"*"<<count<<endl;*/
            if(n==0)
                break;
            count++;
            for(int m=0;m<b_num;m++)
            {
                if(b[m][0]==E_I && b[m][1]==E_J && b[m][2]==E_K)
                {
                    n=0;break;
                }
            }
        }
        if(count==0) cout<<"Trapped!"<<endl;
        else if(count==1) cout<<"Escaped in 1 minute."<<endl;
        else cout<<"Escaped in "<<count<<" minute(s)."<<endl;
    }

    return 0;
}

[UVA] 10196 - Check The Check

/*20140223 hanting*/
#include <iostream>
using namespace std;
int main()
{
    for(int game=1;game<10000;game++)
    {
        char c[18][18];
        int dot=0;
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                cin>>c[i][j];
                if(c[i][j]=='.') dot++;
            }
        }
        if(dot==64) break;/* 若全都是'.'就結束 */
        cout<<"Game #"<<game<<": ";
        for(int i=0;i<=8;i++)
        {//cout<<endl;
            if(i==8)
            {
                cout<<"no";
                break;
            }
            for(int j=0;j<8;j++)
            {//cout<<i<<"*"<<j<<"*"<<c[i][j];
                if(c[i][j]=='P')
                {
                    if(c[i-1][j-1]=='k' || c[i-1][j+1]=='k')
                    {
                        cout<<"black";
                        i=10;j=8;break;
                    }
                }
                else if(c[i][j]=='p')
                {
                    if(c[i+1][j-1]=='K' || c[i+1][j+1]=='K')
                    {
                        cout<<"white";
                        i=10;j=8;break;
                    }
                }
                else if(c[i][j]=='R')
                {
                    /*R往上*/
                    for(int e=i-1;e>=0;e--)
                    {//c[e][j]='*';
                        if(c[e][j]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*R往下*/
                    for(int e=i+1;e<8;e++)
                    {//c[e][j]='*';
                        if(c[e][j]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*R往左*/
                    for(int e=j-1;e>=0;e--)
                    {//c[i][e]='*';
                        if(c[i][e]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*R往右*/
                    for(int e=j+1;e<8;e++)
                    {//c[i][e]='*';
                        if(c[i][e]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                }
                else if(c[i][j]=='r')
                {
                    /*r往上*/
                    for(int e=i-1;e>=0;e--)
                    {
                        if(c[e][j]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*r往下*/
                    for(int e=i+1;e<8;e++)
                    {
                        if(c[e][j]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*r往左*/
                    for(int e=j-1;e>=0;e--)
                    {
                        if(c[i][e]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*r往右*/
                    for(int e=j+1;e<8;e++)
                    {
                        if(c[i][e]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                }
                else if(c[i][j]=='B')
                {
                    /*B往左上*/
                    for(int e=i-1,f=j-1 ; e>=0&&f>=0 ; e--,f--)
                    {//cout<<"*+*"<<e<<"@"<<f<<"@"<<c[e][f]<<endl;
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*B往右上*/
                    for(int e=i-1,f=j+1 ; e>=0&&f<8 ; e--,f++)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*B往左下*/
                    for(int e=i+1,f=j-1 ; e<8&&f>=0 ; e++,f--)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*B往右下*/
                    for(int e=i+1,f=j+1 ; e<8&&f<8 ; e++,f++)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                }
                else if(c[i][j]=='b')
                {
                    /*b往左上*/
                    for(int e=i-1,f=j-1 ; e>=0&&f>=0 ; e--,f--)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*b往右上*/
                    for(int e=i-1,f=j+1 ; e>=0&&f<8 ; e--,f++)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*b往左下*/
                    for(int e=i+1,f=j-1 ; e<8&&f>=0 ; e++,f--)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*b往右下*/
                    for(int e=i+1,f=j+1 ; e<8&&f<8 ; e++,f++)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                }
                else if(c[i][j]=='Q')
                {
                    /*Q往上*/
                    for(int e=i-1;e>=0;e--)
                    {
                        if(c[e][j]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*Q往下*/
                    for(int e=i+1;e<8;e++)
                    {
                        if(c[e][j]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*Q往左*/
                    for(int e=j-1;e>=0;e--)
                    {
                        if(c[i][e]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*Q往右*/
                    for(int e=j+1;e<8;e++)
                    {
                        if(c[i][e]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*Q往左上*/
                    for(int e=i-1,f=j-1 ; e>=0&&f>=0 ; e--,f--)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*Q往右上*/
                    for(int e=i-1,f=j+1 ; e>=0&&f<8 ; e--,f++)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*Q往左下*/
                    for(int e=i+1,f=j-1 ; e<8&&f>=0 ; e++,f--)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*Q往右下*/
                    for(int e=i+1,f=j+1 ; e<8&&f<8 ; e++,f++)
                    {
                        if(c[e][f]=='k')
                        {
                            cout<<"black";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                }
                else if(c[i][j]=='q')
                {
                    /*q往上*/
                    for(int e=i-1;e>=0;e--)
                    {
                        if(c[e][j]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*q往下*/
                    for(int e=i+1;e<8;e++)
                    {
                        if(c[e][j]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][j]!='.') break;
                    }
                    /*q往左*/
                    for(int e=j-1;e>=0;e--)
                    {
                        if(c[i][e]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*q往右*/
                    for(int e=j+1;e<8;e++)
                    {
                        if(c[i][e]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[i][e]!='.') break;
                    }
                    /*q往左上*/
                    for(int e=i-1,f=j-1 ; e>=0&&f>=0 ; e--,f--)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*q往右上*/
                    for(int e=i-1,f=j+1 ; e>=0&&f<8 ; e--,f++)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*q往左下*/
                    for(int e=i+1,f=j-1 ; e<8&&f>=0 ; e++,f--)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                    /*q往右下*/
                    for(int e=i+1,f=j+1 ; e<8&&f<8 ; e++,f++)
                    {
                        if(c[e][f]=='K')
                        {
                            cout<<"white";
                            i=10;j=8;break;
                        }
                        else if(c[e][f]!='.') break;
                    }
                }
                else if(c[i][j]=='N')
                {
                    if(c[i-1][j-2]=='k' || c[i-1][j+2]=='k' || c[i-2][j-1]=='k' || c[i-2][j+1]=='k'
                        || c[i+1][j-2]=='k' || c[i+1][j+2]=='k' || c[i+2][j-1]=='k' || c[i+2][j+1]=='k')
                    {
                        cout<<"black";
                        i=10;j=8;break;
                    }
                }
                else if(c[i][j]=='n')
                {
                    if(c[i-1][j-2]=='K' || c[i-1][j+2]=='K' || c[i-2][j-1]=='K' || c[i-2][j+1]=='K'
                        || c[i+1][j-2]=='K' || c[i+1][j+2]=='K' || c[i+2][j-1]=='K' || c[i+2][j+1]=='K')
                    {
                        cout<<"white";
                        i=10;j=8;break;
                    }
                }

            }
        }
        cout<<" king is in check."<<endl;
    }
    return 0;
}

2014年2月15日 星期六

[UVA] 357 - Let Me Count The Ways

/*20140216 hanting*/
#include <iostream>
#include <cstring>
using namespace std;
long long a[30001]={1};
int coin[5]={1,5,10,25,50};
int main()
{
    for(int j=0;j<5;j++)
    {
        for(int i=coin[j];i<=30000;i++)
        {
            a[i]+=a[i-coin[j]];
        }
    }
    int n;
    while(cin>>n)
    {
        if(n<5)
        {
            cout<<"There is only 1 way to produce "<<n<<" cents change."<<endl;
            continue;
        }
        cout<<"There are "<<a[n]<<" ways to produce "<<n<<" cents change."<<endl;
    }
    return 0;
}