zoj 1091 Knight Moves
2010-07-17 15:00
363 查看
一直仰望这种骑士(虽然有人说是马。。我觉得骑士好听,嘻嘻)题,哈哈哈~~~~如今做出来啦~~~~欧耶~~~~~!
及其类似抓牛那题。。ZOJ上居然没写骑士要怎么走。。还搜了半天,额,知道怎么走了,“日”字格,和象棋中的马一样。那要有八种情况啊,郁闷。瞟了眼某亮的代码,用了个数组控制方向,然后加个循环!真聪明的办法啊,脑子咋长的。。想起来第一次去ACM实验室的时候孙柯在讲搜索,好像有说过用一个数组控制方向,当时真是啥都不懂。。现在终于明白了,泪奔~~~~~~~~~
经过这三天,队列我是越来越熟啦~嘻嘻,广搜理解了~欧耶~~~向深搜迈进!撒花~~~~~~~~
及其类似抓牛那题。。ZOJ上居然没写骑士要怎么走。。还搜了半天,额,知道怎么走了,“日”字格,和象棋中的马一样。那要有八种情况啊,郁闷。瞟了眼某亮的代码,用了个数组控制方向,然后加个循环!真聪明的办法啊,脑子咋长的。。想起来第一次去ACM实验室的时候孙柯在讲搜索,好像有说过用一个数组控制方向,当时真是啥都不懂。。现在终于明白了,泪奔~~~~~~~~~
经过这三天,队列我是越来越熟啦~嘻嘻,广搜理解了~欧耶~~~向深搜迈进!撒花~~~~~~~~
#include <stdio.h> #include <stdlib.h> #include <string.h> int state[9][9]; int count[9][9]; int Queue[100000]; int step[8][2] = {1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1}; int head,tail; int push(int x) { Queue[head++] = x; } int pop(void) { return Queue[tail++]; } int Qempty(void) { if( head == tail ) return 1; return 0; } void init(void) { head = 0; tail = 0; memset( state,0,sizeof(state) ); memset( count,0,sizeof(count) ); memset( Queue,0,sizeof(Queue) ); } int main(void) { int a,b,x,y,temp,tempa,tempx,ta,tx,i; char ch1,ch2,n; while( scanf("%c%d %c%d%c",&ch1,&x,&ch2,&y,&n)!=EOF ) //这点很纠结,因为有个回车,不再输入一个回车的话,会错 { init(); a = ch1 - 'a' + 1; b = ch2 - 'a' + 1; push(a); push(x); state[a][x] = 1; while( !Qempty() ) { tempa = pop(); tempx = pop(); if( tempa == b && tempx == y) break; for(i=0; i<8; i++)//8个方向,用循环一一调用,这点很值得学习! { ta = tempa + step[i][0]; tx = tempx + step[i][1]; if(state[ta][tx] == 0 && ta>=1 && ta<=8 && tx>=1 && tx<=8 ) { push(ta); push(tx); state[ta][tx] = 1; count[ta][tx] = count[tempa][tempx] + 1; } } } printf("To get from %c%d to %c%d takes %d knight moves./n",ch1,x,ch2,y,count[tempa][tempx]); } system("pause"); return 0; }
相关文章推荐
- ZOJ-1091-Knight Moves
- zoj 1091 Knight Moves
- UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves
- BFS例题 ZOJ——1091 Knight Moves
- zoj 1091 Knight Moves
- zoj1091 Knight Moves
- zoj 1091 Knight Moves
- 单源最短路径 ZOJ 1091 Knight Moves
- zoj 1091 Knight Moves
- ZOJ1091 Knight Moves
- zoj 1091 Knight Moves
- zoj1091 Knight Moves
- ZOJ-1091-Knight Moves
- ZOJ-1091-Knight Moves
- ZOJ 1091 POJ 2935 Knight Moves BFS水题
- ZOJ_1091-Knight Moves
- zoj 1091 Knight Moves
- zoj 1091 bfs
- zoj - 1091 - Knight Moves(广度优先法)
- zoj 1091 knight moves (bfs)