标签:iostream nod visit bsp eof ios else 剪枝 str
1 # include <stdio.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 char map[8][8]; 8 int visit[8][8]; 9 struct node{ 10 int x,y; 11 }; 12 int n,m; 13 node Save,Dog;//开始的点和出口位置 14 int step;//记录总的 步数/时间 15 16 int flag = 0; 17 void dfs(int x,int y,int num){ 18 visit[x][y] = 1; 19 if(map[x][y] == ‘D‘){ 20 if(num == step) 21 flag = 1; 22 return; 23 } 24 //剪枝 25 int tmp = step - num - abs(Dog.x-x) - abs(Dog.y-y); 26 if(tmp < 0 || tmp&1){ 27 //cout<<x<<" "<<y<<" "<<time<<" "<<abs(c-x)<<" "<<abs(d-y)<<endl; 28 return; 29 } 30 if(num>step){ 31 return; 32 } 33 int i; 34 int a[4] = {0,0,1,-1}; 35 int b[4] = {-1,1,0,0}; 36 for(i=0;i<4;i++){ 37 int xx = x+a[i]; 38 int yy = y+b[i]; 39 if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!=‘X‘&&!visit[xx][yy]){ 40 dfs(xx,yy,num+1); 41 if(flag) return; 42 visit[xx][yy] = 0; 43 } 44 } 45 //return; 46 } 47 int main(){ 48 //freopen("in.txt","r",stdin); 49 while(cin>>n>>m>>step){ 50 int i,j; 51 if(n==0&&m==0&&step==0) break; 52 for(i=0;i<n;i++){ 53 for(j=0;j<m;j++){ 54 cin>>map[i][j]; 55 if(map[i][j]==‘S‘){ 56 Save.x = i; 57 Save.y = j; 58 } 59 if(map[i][j]==‘D‘){ 60 Dog.x = i; 61 Dog.y = j; 62 } 63 } 64 } 65 66 flag = 0; 67 memset(visit,0,sizeof(visit)); 68 dfs(Save.x,Save.y,0); 69 70 if(flag==1) cout<<"YES"<<endl; 71 else{ 72 cout<<"NO"<<endl; 73 } 74 } 75 }
HDU1010 Tempter of the Bone(DFS+剪枝)
标签:iostream nod visit bsp eof ios else 剪枝 str
原文地址:https://www.cnblogs.com/MAX-ZMY/p/11562058.html