UVa 225 - Golygons(DFS+回溯)
2014-12-05 16:00
351 查看
在一个平面上从原点出发,走n步,第i步走的距离为i,每一步都必须转向90度。有k个障碍物,不能穿过。给出步数和障碍物位置,问有多少种可行路径。
输入有负数,但仍然可以使用数组,将g[maxn][maxn]作为原点。(x,y)用g[maxn+x][maxn+y]表示。
进行DFS,注意是否有障碍物,当剩下的步数走不回原点时,回溯。
输入有负数,但仍然可以使用数组,将g[maxn][maxn]作为原点。(x,y)用g[maxn+x][maxn+y]表示。
进行DFS,注意是否有障碍物,当剩下的步数走不回原点时,回溯。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdlib> using namespace std; const int maxm=25,maxn=250; const char s[]="ensw"; const int go[2][4]={{1,0,0,-1},{0,1,-1,0}}; int n,cnt,sum[maxm]; bool g[maxn*2][maxn*2],vis[maxn*2][maxn*2]; char s0[maxm]; void read(){ int k; cin>>n>>k; while(k--){ int x,y; cin>>x>>y; if(abs(x)>=maxn||abs(y)>=maxn) continue; g[maxn+x][maxn+y]=true;//g[maxn+x][maxn+y]表示(x,y)。 } return; } bool cdt(int x1,int y1,int x2,int y2){//判断是否经过障碍物。 if(x1==x2){ int x=x1+maxn,maxy=maxn+max(y1,y2); for(int y=maxn+min(y1,y2);y<=maxy;y++){ if(g[x][y]) return true; } } if(y1==y2){ int y=y1+maxn,maxx=maxn+max(x1,x2); for(int x=maxn+min(x1,x2);x<=maxx;x++){ if(g[x][y]) return true; } } return false; } void dfs(int cur,int last_x,int last_y,int last_dir){ int dist=abs(last_x)+abs(last_y); if(dist>sum -sum[cur]) return;//剩下步数回不到原点,回溯。 if(cur==n){ if(dist) return; cout<<s0<<endl; cnt++; return; } for(int i=0;i<4;i++){ if(i==last_dir||i==3-last_dir) continue; int cur_x=last_x+go[0][i]*(cur+1),cur_y=last_y+go[1][i]*(cur+1); if(cdt(last_x,last_y,cur_x,cur_y)) continue; if(!vis[maxn+cur_x][maxn+cur_y]){ s0[cur]=s[i]; vis[maxn+cur_x][maxn+cur_y]=true; dfs(cur+1,cur_x,cur_y,i); vis[maxn+cur_x][maxn+cur_y]=false; s0[cur]=0; } } return; } int main(){ ios::sync_with_stdio(false); int t; cin>>t; for(int i=1;i<maxm;i++) sum[i]=sum[i-1]+i; while(t--){ cnt=0; memset(vis,0,sizeof(vis)); memset(s0,0,sizeof(s0)); memset(g,0,sizeof(g)); read(); if((n>14&&n<17)||(n>6&&n<9))//n不为这些值时,没有解。 dfs(0,0,0,-1); cout<<"Found "<<cnt<<" golygon(s)."<<endl<<endl; } return 0; }
相关文章推荐
- UVa 225 - Golygons ( DFS, 回溯, 剪枝 )
- Uva225 Golygons 【dfs回溯】【习题7-2】
- UVA 225 - Golygons(dfs回溯)
- UVA 225 - Golygons(dfs回溯)
- UVA225 Golygons 黄金图形(dfs+回溯)
- 【uva】225 - Golygons(dfs有坑)
- UVa 225 - Golygons (回溯 + 剪枝)
- UVA225 Golygons 【搜索, dfs】
- UVA 225 Golygons(DFS + sort)
- UVa 225 – Golygons [DFS+剪枝]
- UVa 225 – Golygons [DFS+剪枝]
- UVa 225:Golygons(DFS)
- UVA 225 Golygons (DFS)
- 紫书搜索 习题7-2 UVA - 225 Golygons 搜索dfs
- UVa 225 黄金图形(回溯+剪枝)
- UVA - 225 Golygons
- UVA --11218 KTV(DFS+回溯)
- UVa 225 - Golygons
- Uva129 Krypton Factor【dfs回溯】【例题7-5】
- Uva11214 Guarding the Chessboard【dfs回溯】【习题7-10】