keymasterviriya1150

ม้าหมากรุก ACM-ICPC Online 2012

Jun 18th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // ACM-ICPC Online 2012 ม้าหมากรุก
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<sstream>
  5. #include<fstream>
  6. #include<utility>
  7. #include<cstdlib>
  8. #include<cstring>
  9. #include<string>
  10. #include<bitset>
  11. #include<vector>
  12. #include<cstdio>
  13. #include<cctype>
  14. #include<cmath>
  15. #include<queue>
  16. #include<deque>
  17. #include<stack>
  18. #include<map>
  19. #include<climits>
  20. #include <utility>
  21. #define ll long long
  22. #define p cout
  23. #define s cin
  24. #define pi 2*acos(0.0)
  25. #define fuck INT_MAX
  26. using namespace std;
  27. void ch(int i,int j,int a[100][100],int N)
  28. {
  29.  if(a[i-2][j+1]==fuck && i-2>=1 && j+1<=N)
  30.     a[i-2][j+1]=a[i][j]+1;
  31.  
  32.  if(a[i-2][j-1]==fuck && i-2>=1 && j-1>=1)
  33.     a[i-2][j-1]=a[i][j]+1;
  34.  
  35.  if(a[i+2][j+1]==fuck && i+2<=N && j+1<=N)
  36.     a[i+2][j+1]=a[i][j]+1;
  37.  
  38. if(a[i+2][j-1]==fuck && i+2<=N && j-1>=1)
  39.      a[i+2][j-1]=a[i][j]+1;
  40.  
  41.  if(a[i-1][j+2]==fuck && i-1>=1 && j+2<=N)
  42.    a[i-1][j+2]=a[i][j]+1;
  43.  
  44.  if(a[i-1][j-2]==fuck && i-1>=1 && j-2>=1)
  45.     a[i-1][j-2]=a[i][j]+1;
  46.  
  47.  if(a[i+1][j+2]==fuck && i+1<=N && j+2<=N)
  48.     a[i+1][j+2]=a[i][j]+1;
  49.  
  50.  if(a[i+1][j-2]==fuck && i+1<=N && j-2>=1)
  51.     a[i+1][j-2]=a[i][j]+1;
  52.  
  53. }
  54. int main()
  55. {
  56.     int N,M,P;
  57.     short number1,number2;
  58.    
  59.     while(1)
  60.     {
  61.         s >> N >> M >> P;
  62.         if(N==0 && M==0 && P==0)
  63.             break;
  64.         int a[100][100];
  65.        
  66.         vector<pair<int,int> > v ;
  67.         v.push_back(make_pair(0,0));
  68.        
  69.         vector<pair<int,int> > v1 ;
  70.         v1.push_back(make_pair(0,0));
  71.        
  72.         for(short i=0;i<=M;i++)
  73.         {
  74.             s >> number1 >> number2;
  75.             v.push_back(make_pair(number1,number2));
  76.         }
  77.    
  78.         for(short i=0;i<P;i++)
  79.         {
  80.             s >> number1 >> number2;
  81.             v1.push_back(make_pair(number1,number2));
  82.         }
  83.    
  84.         for(short i=1;i<=N;i++)
  85.         {
  86.             for(short j=1;j<=N;j++)
  87.             {
  88.                 if(v[1].first==i && v[1].second==j)
  89.                         a[i][j]=0;
  90.                 else  a[i][j]=fuck;
  91.             }
  92.         }
  93.        
  94.        
  95.         for(int k=0;k<=N;k++)
  96.         {                
  97.             for(int i=1;i<=N;i++)  
  98.             {
  99.                 for(int j=1;j<=N;j++)
  100.                 {
  101.                     if(a[i][j]==k)
  102.                         ch(i,j,a,N);
  103.                 }
  104.             }
  105.         }
  106.         for(int i=1;i<=M;i++)  
  107.             a[v[i].first][v[i].second]=-1;
  108.            
  109.         for(int i=1;i<v1.size();i++)
  110.         {
  111.                 cout << a[v1[i].first][v1[i].second]<<endl;
  112.         }
  113.        
  114.     }
  115.  
  116.    
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment