标签:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 22486 | Accepted: 10511 |
Description
Input
Output
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
Source
广度优先
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 int s; 6 struct point{ 7 bool ex; 8 int step; 9 }; 10 point m[300+5][300+5]; 11 int d[8][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}}; 12 bool check(int x,int y){ 13 if(x<0||x>=s||y<0||y>=s||m[x][y].ex) 14 return true; 15 return false; 16 } 17 int main(){ 18 int n,sx,sy,ex,ey; 19 cin>>n; 20 while(n--){ 21 cin>>s; 22 cin>>sx>>sy; 23 cin>>ex>>ey; 24 memset(m,false,sizeof(m)); 25 m[sx][sy].ex=true; 26 m[sx][sy].step=0; 27 queue<int> q; 28 q.push(sx*1000+sy); 29 int t=0; 30 while(!q.empty()){ 31 int x=q.front()/1000; 32 int y=q.front()%1000; 33 q.pop(); 34 if(x==ex&&y==ey){ 35 break; 36 } 37 int i=0; 38 for(;i<8;i++){ 39 int xx=x+d[i][0]; 40 int yy=y+d[i][1]; 41 if(check(xx,yy)){ 42 continue; 43 } 44 q.push(xx*1000+yy); 45 m[xx][yy].ex=true; 46 m[xx][yy].step=m[x][y].step+1; 47 } 48 } 49 cout<<m[ex][ey].step<<endl; 50 } 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4280341.html