Pages

শুক্রবার, ৩ জানুয়ারী, ২০১৪

Solution code of uva 928-Eternal truth


#include<cstdio>
#include<iostream>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#define pa pair<int,int>
#define inf 0x7fffffff
using namespace std;
int row,col,sor_x,sor_y,end_x,end_y;
int main()
{
  // freopen("c:\\temp\\in.txt","r",stdin);
 int test_case;
 scanf("%d",&test_case);
 int a_x[]={-1,1,0,0};
 int a_y[]={0,0,1,-1};
 while(test_case--)
 {
      char data[301][301];
     int visit[301][301][4];
       scanf("%d %d",&row,&col);
     for(int i=1;i<=row;i++)
     {
         for(int j=1;j<=col;j++)
         {
             cin>>data[i][j];
             if(data[i][j]=='S')
                sor_x=i,sor_y=j;
             if(data[i][j]=='E')
                end_x=i,end_y=j;
                visit[i][j][0]=visit[i][j][1]=visit[i][j][2]=visit[i][j][3]=inf;
         }
     }
     queue< pair<pa,int> >my;
     my.push(make_pair(make_pair(sor_x,sor_y),0));
     visit[sor_x][sor_y][0]=0;
     int found=0;
     int p;
     int next_x,next_y;

     while(!my.empty() && !found)
     {
         int r=my.front().first.first;
         int c=my.front().first.second;
         int jump=my.front().second;
           p=jump%3+1;
           my.pop();
         for(int i=0;i<4;i++)
         {
             next_x=r+a_x[i]*p;
             next_y=c+a_y[i]*p;
            if(next_x>=1 && next_x<=row && next_y>=1 && next_y<=col && data[next_x][next_y]!='#' && visit[next_x][next_y][p]==inf)
            {
                int check=1;
                for(int t=1;t<=p && check;t++)
                {
                      if(data[r+a_x[i]*t][c+a_y[i]*t]=='#')
                        check=0;
                }
                if(check)
                {
                      visit[next_x][next_y][p]=visit[r][c][jump]+1;
                       my.push(make_pair(make_pair(next_x,next_y),p));
                if(next_x== end_x && next_y==end_y)
                    {
                        found=true;break;
                    }

                }
            }
         }
    }
    if(!found)
        cout<<"NO\n";
    else cout<<visit[next_x][next_y][p]<<endl;
 }
 return 0;
}

1 comments: