poj2243 hdu1372 floyid warshall
来源:互联网 发布:网上看房用什么软件 编辑:程序博客网 时间:2024/05/21 06:46
可以通过图的连通性进行求解。floyid算法也是比较好理解的。但和bfs比较它的空间复杂度较高。
#include <iostream> #include <string> using namespace std; const int chess = 9; int w[chess][chess][chess][chess]; bool position(int x,int y) { if(x > 0 && x < chess && y >0 && y <chess) return true; return false; } void init() { memset(w,-1,sizeof(w)); for(int x = 1;x < chess;x++) for(int y = 1;y < chess;y++) { if(position(x + 1,y + 2)) w[x][y][x + 1][y + 2] = 1; if(position(x + 2,y + 1)) w[x][y][x + 2][y + 1] = 1; if(position(x - 1,y + 2)) w[x][y][x - 1][y + 2] = 1; if(position(x -2,y +1)) w[x][y][x -2][y +1] = 1; if(position(x + 1,y - 2)) w[x][y][x + 1][y - 2] = 1; if(position(x + 2,y -1)) w[x][y][x + 2][y - 1] = 1; if(position(x - 1,y - 2)) w[x][y][x - 1][y - 2] = 1; if(position(x -2,y -1)) w[x][y][x -2][y -1] = 1; }//for int y for(int kx = 1;kx < chess;kx++) for(int ky = 1; ky < chess;ky++) for(int ix = 1;ix < chess ;ix++) for(int iy = 1;iy < chess;iy++) for(int jx = 1;jx < chess;jx++) for(int jy = 1;jy < chess;jy++) { if(w[ix][iy][jx][jy] > w[ix][iy][kx][ky] + w[kx][ky][jx][jy] && w[ix][iy][kx][ky] >0 && w[kx][ky][jx][jy] > 0) w[ix][iy][jx][jy] = w[ix][iy][kx][ky] + w[kx][ky][jx][jy]; else if(-1==w[ix][iy][jx][jy] && w[ix][iy][kx][ky] >0 && w[kx][ky][jx][jy] > 0 ) w[ix][iy][jx][jy] = w[ix][iy][kx][ky] + w[kx][ky][jx][jy]; } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); string s1,s2; int res; init(); while(cin>>s1>>s2) { int x,y,m,n; x = s1[0] - 'a' + 1; y = s1[1] - '0'; m = s2[0] - 'a' + 1; n = s2[1] - '0'; if(x==m && y==n) res = 0; else res = w[x][y][m][n]; cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<res<<" knight moves."<<endl; } return 0; }
0 0
- poj2243 hdu1372 floyid warshall
- poj2243 && hdu1372 Knight Moves(BFS)
- UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves
- POJ2243
- poj2243
- poj2243
- poj2243
- poj2243
- hdu1372
- hdu1372
- hdu1372
- HDU1372
- HDU1372
- HDU1372
- warshall
- HDU1372 BFS
- HDU1372&&POJ1915
- HDU1372搜索
- codeforces 431B Shower Line
- 第9周项目1-利用循环程序求和B
- AIX上Sybase15的安装
- cocos2dx A* + tiledMap(改良升级)
- Matlab之BP神经网络反向传播算法
- poj2243 hdu1372 floyid warshall
- 大学规划
- ios 如何获取不变的UDID
- ubuntu 上安装 SDL
- andriod中如果引入jar包错误出现Conversion to Dalvik format failed with error 1
- linux环境下规定时间内进行数据库备份
- 深入浅出Java垃圾回收机制(2)——如何监控Java垃圾回收机制
- PHP实现的ε-NFA
- 第9周项目1C-利用循环求和C