POJ 1915 简单 广搜题
2012-10-30 14:53
274 查看
题意要求是给出两个坐标,给出一个棋盘的大小,问用最小的步数 从初始坐标走到最后终点,
此题是典型的广搜题,但自己大意了,忘记把 标记数组visisted【】【】初始化了,以致于错了很多次,
就当给自己长教训吧
#include<iostream>
#include<queue>
#include<fstream>
using namespace std;
int bmp[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int visited[310][310];
int N,T,p;
struct node{
int x;
int y;
int temp;
};
queue <node> vi;
node first,next,last,top,jian;
bool cheak(int a, int b)
{
if(a >= 0 && a < T && b >= 0 && b < T )
return true;
return false;
}
void bfs(int x,int y)
{
while(!vi.empty())
vi.pop();
//p = 0;
top.x = x;
top.y = y;
top.temp = 0;
vi.push(top);
visited[top.x][top.y] = 1;
while(!vi.empty())
{
jian = vi.front();
vi.pop();
for(int i = 0;i < 8; i++)
{
next.x = jian.x + bmp[i][0];
next.y = jian.y + bmp[i][1];
next.temp = jian.temp;
if(cheak(next.x ,next.y) && !visited[next.x][next.y] )
{
if(next.x == last.x && next.y == last.y)
{
p = next.temp+1;
return;
}
next.temp++;
visited[next.x][next.y] = 1;
vi.push(next);
}
}
}
}
int main()
{
ifstream cin("in.txt");
cin>>N;
while(N--)
{
memset(visited,0,sizeof(visited));
cin>>T;
cin>>first.x>>first.y>>last.x>>last.y;
first.temp = 0;
if(first.x == last.x && first.y == last.y)
p = 0;
else
bfs(first.x,first.y);
cout<<p<<endl;
}
}
此题是典型的广搜题,但自己大意了,忘记把 标记数组visisted【】【】初始化了,以致于错了很多次,
就当给自己长教训吧
#include<iostream>
#include<queue>
#include<fstream>
using namespace std;
int bmp[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int visited[310][310];
int N,T,p;
struct node{
int x;
int y;
int temp;
};
queue <node> vi;
node first,next,last,top,jian;
bool cheak(int a, int b)
{
if(a >= 0 && a < T && b >= 0 && b < T )
return true;
return false;
}
void bfs(int x,int y)
{
while(!vi.empty())
vi.pop();
//p = 0;
top.x = x;
top.y = y;
top.temp = 0;
vi.push(top);
visited[top.x][top.y] = 1;
while(!vi.empty())
{
jian = vi.front();
vi.pop();
for(int i = 0;i < 8; i++)
{
next.x = jian.x + bmp[i][0];
next.y = jian.y + bmp[i][1];
next.temp = jian.temp;
if(cheak(next.x ,next.y) && !visited[next.x][next.y] )
{
if(next.x == last.x && next.y == last.y)
{
p = next.temp+1;
return;
}
next.temp++;
visited[next.x][next.y] = 1;
vi.push(next);
}
}
}
}
int main()
{
ifstream cin("in.txt");
cin>>N;
while(N--)
{
memset(visited,0,sizeof(visited));
cin>>T;
cin>>first.x>>first.y>>last.x>>last.y;
first.temp = 0;
if(first.x == last.x && first.y == last.y)
p = 0;
else
bfs(first.x,first.y);
cout<<p<<endl;
}
}
相关文章推荐
- POJ-1915 Knight Moves 简单搜索
- poj 1915 Knight Moves【BFS】【简单】
- poj 1915 knight moves(bfs 简单)
- 超超超简单的bfs——POJ-1915
- poj 1915//简单bfs
- poj1915 简单bfs
- POJ 2723 - Get Luffy Out 读清题意~简单二分2sat
- Sumsets (poj 2229 简单dp)
- POJ 1286 Necklace of Beads(Polya简单应用)
- poj 1979 Red and Black (简单裸搜索)
- poj2388 简单排序——归并排序
- POJ 3299(湿润指数 简单数学) 解题报告
- 【POJ 3669 Meteor Shower】简单BFS
- 简单几何(凸包) POJ 1113 Wall
- poj 2011 Shortest Prefixes 简单字典树
- POJ2488——简单DFS
- poj 1260(简单动态规划)
- POJ 1321 棋盘问题---[kuangbin带你飞]专题一 简单搜索
- POJ 3616【数状数组求区间最大值 + 简单DP】
- poj 3616(简单dp)