再切一道题,冲进前10000名!--POJ 1915
又是细节---我都花费了一个小时在找一个很小很小的东西,TMD,受不了!!
Source Code
Problem: 1915 | User: omycle | |
Memory: 1324K | Time: 94MS | |
Language: C++ | Result: Accepted |
- Source Code
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAXSIZE 90000//队列的极限情况
//
typedef struct
{
int i;
int j;
}qipan;
qipan duilie[MAXSIZE+1];
int step[MAXSIZE+1];
bool flag[MAXSIZE+1];
int tou=0;
int wei=0;
int c[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int jisuan(int i,int j)
{
return i*300+j;
}
void Play()
{
int n=0;
scanf("%d",&n);
while(n>0)
{
//将数组初始化
memset(step,0,(MAXSIZE+1)*sizeof(int));
memset(flag,false,(MAXSIZE+1)*sizeof(bool));
for(int i=0;i<=MAXSIZE;i++)
{
duilie[i].i=0;
duilie[i].j=0;
}
//变量初始化
int tou=0;
int wei=1;
int max=0;//棋盘的规模
step[tou]=0;
scanf("%d",&max);
int i_input,j_input;
scanf("%d%d",&i_input,&j_input);
duilie[tou].i=i_input;
duilie[tou].j=j_input;
flag[jisuan(i_input,j_input)]=true;
int final_i,final_j;
scanf("%d%d",&final_i,&final_j);
if(duilie[tou].i==final_i&&duilie[tou].j==final_j)
{
int d=0;
printf("%d\n",d);
// return; ////就是这句话!!!
n--;
continue;
}
bool fl=false;
while(wei>tou)
{
int i_temp=duilie[tou].i;
int j_temp=duilie[tou].j;
for(int i=0;i<8;i++)
{
i_temp=duilie[tou].i;
j_temp=duilie[tou].j;
i_temp+=c[i][0];
j_temp+=c[i][1];
if(i_temp>=0&&i_temp<max&&j_temp>=0&&j_temp<max&&(!flag[jisuan(i_temp,j_temp)]))
{
flag[jisuan(i_temp,j_temp)]=true;
//放入队列的尾部
duilie[wei].i=i_temp;
duilie[wei].j=j_temp;
//步数加1
step[wei]=step[tou]+1;
//如果到达终点,那么就OK!
if(i_temp==final_i&&j_temp==final_j)
{
printf("%d\n",step[wei]);
fl=true;
break;
}
wei++;
}
}
if(fl)
break;
tou++;
}
n--;
}
}
int main()
{
Play();
return 0;
}
评论
发表评论