- 浏览: 198055 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (137)
- c++ (74)
- c++,算法,回溯 (2)
- DP问题。 (9)
- DP问题,0/1背包问题 (3)
- 数学问题 (6)
- 贪心算法 (10)
- 排序 (16)
- 数据结构 (7)
- 容器 (2)
- 模拟问题 (2)
- 水题 (8)
- 并查集 (3)
- 非技术 (2)
- 素数问题 (1)
- DFS (3)
- 二叉树 (1)
- 递归 (1)
- 图论 (5)
- 最小生成树 (5)
- 最短路径 (6)
- bell_flaod算法 (2)
- hash (3)
- 二分查找 (1)
- 搜索 (5)
- BFS (5)
- STL (3)
- 字符串hash (1)
- 拓扑排序 (1)
- 字典树 (4)
- 哈弗曼树 (1)
- KMP (7)
- 线段树 (9)
- 树状数组 (6)
- 全排列 (2)
- DP问题 (2)
- LCS (1)
- 最长不下降子序列 (2)
- 面试经验 (3)
具体代码如下:
#include<iostream>
#include<queue>
using namespace std;
struct point
{
int x,y;
int step;
};
int flag[45][45];
char map[45][45];
int dir1[4][2]={{0,-1},{1,0},{0,1},{-1,0}};//zuo优先
int dir2[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//you优先
int d1,d2;
int start[2];
int end[2];
int w,h;
void init()
{
memset(flag,0,sizeof(flag));
cin>>w>>h;
for(int i=0;i<h;i++)
{
cin>>map[i];
for(int j=0;j<w;j++)
{
if(map[i][j]=='#')
{
flag[i][j]=1;
}
if(map[i][j]=='S')
{
start[0]=i;
start[1]=j;
}
if(map[i][j]=='E')
{
end[0]=i;
end[1]=j;
}
}
}
if(start[0]==0)
{
d1=1;
d2=1;
}
else if(start[0]==h-1)
{
d1=3;
d2=3;
}
else if(start[1]==w-1)
{
d1=2;
d2=0;
}
else
{
d1=0;
d2=2;
}
}
int dfs(int x,int y,int d,int dir[][2])
{
int step ,temp, tempx,tempy;
if(x==end[0]&&y==end[1])
return 1;
for(int i=0;i<4;i++)
{
temp=(d+i)%4;
tempx=x+dir[temp][1];
tempy=y+dir[temp][0];
if(tempx>=0&&tempx<h&&tempy>=0&&tempy<w&&!flag[tempx][tempy])
break;
}
step=dfs(tempx,tempy,(temp+3)%4,dir)+1;
return step;
}
int bfs()
{
memset(flag,0,sizeof(flag));
queue<point> q;
point p;
p.x=start[0];
p.y=start[1];
p.step=1;
flag[p.x][p.y]=1;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
if(p.x==end[0]&&p.y==end[1])
return p.step;
for(int i=0;i<4;i++)
{
point temp;
temp.x=p.x+dir1[i][1];
temp.y=p.y+dir1[i][0];
if(temp.x>=0&&temp.x<h&&temp.y>=0&&temp.y<w&&map[temp.x][temp.y]!='#'&&!flag[temp.x][temp.y])
{
flag[temp.x][temp.y]=1;
temp.step=p.step+1;
q.push(temp);
}
}
}
return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
init();
cout<<dfs(start[0],start[1],d1,dir1)<<" ";
cout<<dfs(start[0],start[1],d2,dir2)<<" ";
cout<<bfs()<<endl;
}
}
发表评论
-
虚函数、纯虚函数、虚基类、抽象类、虚函数继承、虚继承
2013-08-29 14:34 795虚函数:虚函数是C++中用于实现多态(polymorphis ... -
排序算法总结
2013-05-17 11:00 798选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法, ... -
poj 3122
2012-12-11 19:51 826题意:作者要开一个生日party,他现在拥有n块高度都为1 ... -
poj 3273
2012-12-11 16:49 957题意:给你天数n,和每天需要花的钱,让你把这些天分成m份(每份 ... -
字典树学习材料
2012-05-30 14:29 943字典树,又称单词查找树,Trie树,是一种树形结构,典型应 ... -
poj 1159
2012-05-28 19:08 1412题目大意:给你一段字符串,让你求出在中间最少加入几个字符 ... -
poj 3176
2012-05-28 14:47 988大致题意: 输入一个n层的三角形,第i层有i个数,求从第 ... -
poj 1260
2012-05-28 09:54 1582题意解释: 有n个等级的珠宝,等级依次升高,等级越高价钱越高 ... -
poj 1836
2012-05-28 09:22 2684是POJ2533的扩展题。题意不难,令到原队列 ... -
poj 2533
2012-05-26 15:36 1221在做这道题目之前,首先让我们了解一下什么是LIS算法,LIS俗 ... -
poj 3267
2012-05-26 09:43 784从程序可以看出,第i个位置到L所删除的字符数,总是先取最坏情况 ... -
poj 1276
2012-05-25 16:20 2365题意: 这道题的意思是给你一堆钱,各种面值的都有,比 ... -
poj 1094
2012-05-25 13:54 1074题意:给出字母个数,和有限个有序对(a<b)求出能确定字 ... -
poj 3393
2012-05-23 17:00 1234大致题意: 科普文一篇,文章80%都是无用信息,因为 ... -
poj 3007
2012-05-14 10:21 962大致题意: 给定一个字符串,从任意位置把它切为两半, ... -
poj 3096
2012-05-10 21:09 975题意: 定义D-pairs表示取字符串s中相距为D的两个字母 ... -
poj 1426
2012-04-26 20:11 2131大致题意: 给出一个整数n,(1 <= n <= ... -
poj 1797
2012-04-24 15:05 1605题目大意是就是何处一个图,n个顶点和m条边,每个边都有最大承载 ... -
poj 1338
2012-04-23 10:20 1234题意:题目意思是求由2,3,5的乘积组成的数从大到小排列,从1 ... -
poj 2021
2012-04-19 15:00 911题意:Ted今年100岁,给出n对他家族的关系:“父 ...
相关推荐
poj 3083解题报告poj 3083解题报告poj 3083解题报告poj 3083解题报告
POJ-2870 Light Up + DFS(1级DFS+1级DFS) + Python - 思维导图 链接:https://blog.csdn.net/xxdragon126/article/details/122095922?spm=1001.2014.3001.5501
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
2505 2521 2538 2546 2551 2590 2593 2601 2665 2680 2739 2752 2761 2762 2777 2800 2891 2893 2992 3030 3041 3132 3159 3187 3204 3270 3277 3281 3297 3321 3414 3436 3461 3650 3663 3664 3672 3740
北大POJ3083-Children of the Candy Corn 解题报告+AC代码
北大POJ1014-Dividing【DFS】【多重背包+二进制优化】 解题报告+AC代码
用DFS深搜,你会发现时间复杂非常高,必然会超时(最大是300*300的图)。本题可以使用改进过的广搜或优先队列+bfs 或 记忆化广搜三种方法来解决。第一种方法:改进过的BFS:有些节点需要耗费2个单位时间,要
北大POJ3026-Borg Maze【BFS+Prim】 解题报告+AC代码
业余爱好。所以,算法不一定好,CODING也不一定佳,效率不一定高,只是能通过online judge而已。
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
poj 800+ 题目源代码,多年做题积累 包含各种类型经典题目
背包问题 动态规划 POJ 3267 POJ 1260 POJ 1015 POJ 3176 POJ 1080 POJ 1159 POJ 2533 POJ 1836 Leetcode 70 Leetcode 309 搜索 DFS POJ 2488 POJ 3083 POJ 3009 POJ 1321 BFS POJ 3278 POJ 1426 POJ 3126 POJ 3414 ...
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
北大POJ1020-Anniversary Cake 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类