sicily 1152 简单的马周游问题
2011-06-11 20:49
531 查看
解法一:
解法二:
// source code of submission 799172, Zhongshan University Online Judge System #include <cstdio> #include <vector> #include <memory.h> #include <algorithm> using namespace std; int dir[8][2] = {-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1}; int vis[5][6],path[30]; struct point { int x, y, next; }; bool is_legal(point p) //判断当前位置是否合法 { if(p.x<0 || p.x>4 || p.y<0 || p.y>5 ||vis[p.x][p.y]) return false; return true; } bool cmp(point a,point b) { return a.next < b.next; } bool dfs(point a,int cur) { if(cur == 30) { for(int i = 0;i < 29;i++) printf("%d ",path[i]); printf("%d/n",path[29]); return true; } else { vector <point> v; for(int i = 0;i < 8;i++) { point p; p.x = a.x + dir[i][0]; p.y = a.y + dir[i][1]; p.next = 0; if(is_legal(p)) //计算该位置的可行拓展数 { for(int j = 0;j < 8;j++) { point n; n.x = p.x + dir[j][0]; n.y = p.y + dir[j][1]; if(is_legal(n)) p.next++; } v.push_back(p); } } sort(v.begin(),v.end(),cmp); for(int ix = 0;ix < v.size();ix++)//优先尝试可行拓展数比较少的,剪枝 { vis[v[ix].x][v[ix].y] = 1; path[cur] = v[ix].x * 6 + v[ix].y + 1; if(dfs(v[ix],cur+1)) return true; vis[v[ix].x][v[ix].y] = 0; } } return false; } int main() { int n; while(scanf("%d",&n),n!=-1) { memset(vis,0,sizeof(vis)); point s; path[0] = n; n -= 1; s.x = n / 6; s.y = n % 6; vis[s.x][s.y] = 1; dfs(s,1); } return 0; }
解法二:
// source code of submission 799263, Zhongshan University Online Judge System #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; int dir[8][2] = {-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1}; int vis[5][6],ans[30]; bool is_legal(int x,int y) //判断该位置是否合法 { if(x<0 || x>=5 || y<0 || y>=6) return false; return true; } int calculate(int x ,int y)//计算当前节点的可行拓展数 { int cnt = 0; for(int i = 0;i < 8;i++) { int X = x + dir[i][0]; int Y = y + dir[i][1]; if(is_legal(X,Y) && !vis[X][Y]) cnt++; } return cnt; } struct coordinate { int x,y,cnt; coordinate(int X,int Y) { x = X; y = Y; cnt = calculate(X,Y); } }; bool cmp(coordinate a,coordinate b) { return a.cnt < b.cnt; } bool dfs(int x,int y ,int cur) { vis[x][y] = 1; vector<coordinate> v; ans[cur] = x * 6 + y +1; if(cur == 29) return true; for(int i = 0;i < 8;i++) { int X = x + dir[i][0]; int Y = y + dir[i][1]; if(is_legal(X,Y) && !vis[X][Y]) { v.push_back(coordinate(X,Y)); } } sort(v.begin(),v.end(),cmp); for(int ix = 0;ix < v.size();ix++)//优先尝试可行拓展数比较少的,剪枝 { if(dfs(v[ix].x,v[ix].y,cur+1)) return true; } vis[x][y] = 0; return false; } int main() { int n,x,y,i; while(scanf("%d",&n),n!=-1) { n -= 1; memset(vis,0,sizeof(vis)); x = n / 6; y = n % 6; dfs(x,y,0); printf("%d",ans[0]); for(i = 1;i < 30;i++) printf(" %d",ans[i]); printf("/n"); } return 0; }
相关文章推荐
- Sicily 1152. 简单的马周游问题
- Sicily 1152 简单的马周游问题
- Sicily 1152.简单的马周游问题
- <OJ_Sicily>1152简单的马周游问题
- Sicily 1152. 简单的马周游问题
- Sicily 1152 简单的马周游问题[Speical judge]
- Sicily 1152 简单的马周游问题[Special judge]
- sicily 1152. 简单的马周游问题
- sicily 1152 简单的马周游问题 and sicily 1153 马的周游问题
- sicily 1152 简单的马周游问题 and sicily 1153 马的周游问题
- [sicily]1152. 简单的马周游问题
- sicily 1152. 简单的马周游问题
- Sicily 1152 简单的马周游问题
- sicily 1152. 简单的马周游问题[Special judge]
- Sicily 1152 & 1153 简单的马周游问题
- sicily 1152 简单的马周游问题
- sicily 1152.简单的马周游问题
- Sicily 1153 马的周游问题[Special judge] && sicily 1152
- 马周游问题【sicily 1152 &1153.】
- Sicily1152 马周游问题