摘要: 给一个8*8的棋盘,给出骑士的起点和终点,求出从起点到终点的走的最小步数。这个也是ZOJ 的1091题。 用bfs解决,一个vis数组保存是否访问过,dis数组保存从起点到该点的移动次数。代码如下:View Code 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 queue<int> q; 7 int d[][2] = {{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, - 阅读全文
posted @ 2013-04-25 17:00 xiaobaibuhei 阅读(173) 评论(0) 推荐(0)
摘要: 给一个m*n的矩阵,数字是0或是1,统计其中八连块的个数,和《算法竞赛入门经典》6.4.1黑白图像一样,应该是那个的原型吧。 代码如下:View Code 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 105; 5 int mat[maxn][maxn], vis[maxn][maxn]; 6 7 void dfs(int x, int y) 8 { 9 if(vis[x][y] || !mat[x][y]) return;10 vis[x][y] = 1;11 dfs(x-1... 阅读全文
posted @ 2013-04-25 15:41 xiaobaibuhei 阅读(204) 评论(0) 推荐(0)
摘要: 给一个先序遍历序列,构建一个二叉树,在垂直方向上给二叉树分列,计算每一列的和。 开始我按照这个序列构建了一个二叉树,然后遍历进行计算,代码如下:View Code 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 5 const int maxn = 199; 6 const int mid = 100; 7 8 struct Node 9 { 10 int value; 11 Node *left, *right; 12 }; 13 14 Node * new_no... 阅读全文
posted @ 2013-04-25 13:28 xiaobaibuhei 阅读(248) 评论(0) 推荐(0)