CSU 1460: Kastenlauf
来源:互联网 发布:linux退出全屏终端 编辑:程序博客网 时间:2024/05/16 13:38
CSU 1460: Kastenlauf
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 110 Solved: 35
[Submit][Status][Web Board]
Description
Input
Output
Sample Input
220 01000 01000 10002000 100020 01000 02000 10002000 2000
Sample Output
happysad
思路分析:
题目的意思是说在一个矩形城市中,有一个人要从一点到达另一点,而且这个人每隔50m就要喝一瓶饮料,要不然就会死,路途中有若干个商店提供饮料,出发时该人身上有20瓶饮料,他经过商店时最多可以买20瓶,问这个人能否到达目的地(这个人要按照Manhattan距离走,不能走对角线)。
基本的搜索题,从起点到终点搜索,即DFS和BFS,DFS可由递归或者非递归(栈结构)实现,BFS由队列实现。要注意的就是跳出搜索的条件要把握好,否则会一直搜下去。。。。。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
int x;
int y;
}a[120],b;
int visited[120];//访问标记
int n;
void dfs_recursion(int m)
{
if (visited[m])
return;//如果该点被访问过则返回上一层递归或跳出递归
visited[m]=1;
for (int i=0;i<n+2;i++)
if (abs(a[m].x-a[i].x)+abs(a[m].y-a[i].y) <= 20)//如果满足曼哈顿距离不大于20m,则搜索下一个点
dfs_recursion(i);
}
int dfs_stack(int m)
{
stack<node> s;
int i;
struct node p;
visited[m]=1;
s.push(a[m]);
while (!s.empty())
{
if (visited[n+1])
return 1;
p=s.top();
for (i=1;i<n+2;i++)
{
if (visited[i] == 0 && abs(p.x-a[i].x)+abs(p.y-a[i].y) <= 20)
{
visited[i]=1;
s.push(a[i]);
break;//如果按顺序找到了和p点满足题目条件的一个点,则跳出该循环,并将找到的该点置成下一次循环的p点(栈顶元素)
}
}
if (i == n+2)
s.pop();//当栈顶的点不能与其他未被访问的所有点满足题目条件,则把该点弹出
}
return 0;
}
int bfs(int m)
{
queue<node> q;
int i;
struct node p;
visited[m]=1;
q.push(a[m]);
while (!q.empty())
{
if (visited[n+1])
return 1;
p=q.front();
q.pop();
for (i=1;i<n+2;i++)
if (visited[i] == 0 && abs(p.x-a[i].x)+abs(p.y-a[i].y) <= 20)
{
visited[i]=1;
q.push(a[i]);//将与点p满足条件的所有未被访问的点都找出来,然后依次入队列
}
}
return 0;
}
int main()
{
int t;
while (scanf("%d",&t) != EOF)
{
while (t--)
{
int l,k;
scanf("%d",&n);
for (int i=0;i<n+2;i++)
{
cin>>l>>k;
a[i].x=l/50;
a[i].y=k/50;//先对数据预处理
}
memset(visited,0,sizeof(visited));
if (bfs(0) == 1)
cout<<"happy"<<endl;
else
cout<<"sad"<<endl;
}
}
return 0;
}
- csu 1460: Kastenlauf
- CSU 1460: Kastenlauf
- CSU1460: Kastenlauf(DFS)
- cdoj 15 Kastenlauf dfs
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- HDU 2059 龟兔赛跑
- Android的onMeasure和onLayout And MeasureSpec揭秘
- iOS中百度地图API的总结
- 二叉树的深度
- 创建自定义 AngularJS 指令:Part 7 Creating a Unique Value Directive using $asyncValidators
- CSU 1460: Kastenlauf
- 黑马程序员_Java基础_集合(1)
- 關於Excel函數
- SSL/TLS -- 网络通信的马奇诺防线?
- HDU-1004-Let the Balloon Rise(直接new一个字符串数组compareTo!)
- Eclipse导入和调试 Android 5.0源码
- 鸡皮肤怎么去除
- POJ 3121 The SetStack Computer
- 在jsp里用getProperty来调用方法