2016.6.20
2016-06-20 20:55
281 查看
进入考试周...终于要开始复习程设了
先重新熟练一下bfs,找了道经典的题目:
红与黑(题目链接:http://noi.openjudge.cn/ch0205/1818/)
总结一下bfs基本题型的做题要素:
1.使用队列,大部分情况下可以无脑写出来)
2.别忘了visited数组,记录哪些地方到过
3.dx[4],dy[4]数组,4次循环。
4.要设一个结构体,存点的坐标,在做找最短路径时还要记录走的步数。
贴代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int x, y;
point(int x, int y) :x(x), y(y){}
};
int w, h;
int ans;
char map[25][25];
bool reach[25][25] = { false };
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
int main()
{
while (cin >> w >> h)
{
if (w == 0 && h == 0)
break;
queue<point> q;
ans = 0;
for (int i = 0; i < 25; ++i)
for (int j = 0; j < 25; ++j)
reach[i][j] = false;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
{
cin >> map[i][j];
if (map[i][j] == '@')
{
map[i][j] = '.';
reach[i][j] = true;
ans++;
q.push(point(i, j));
}
if (map[i][j] == '#')
{
reach[i][j] = true;
}
}
while (!q.empty())
{
point tmp = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if (xx > 0 && xx <= h && yy > 0 && yy <= w && !reach[xx][yy])
{
reach[xx][yy] = true;
ans++;
q.push(point(xx, yy));
}
}
}
cout << ans << endl;
}
return 0;
}
先重新熟练一下bfs,找了道经典的题目:
红与黑(题目链接:http://noi.openjudge.cn/ch0205/1818/)
总结一下bfs基本题型的做题要素:
1.使用队列,大部分情况下可以无脑写出来)
2.别忘了visited数组,记录哪些地方到过
3.dx[4],dy[4]数组,4次循环。
4.要设一个结构体,存点的坐标,在做找最短路径时还要记录走的步数。
贴代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int x, y;
point(int x, int y) :x(x), y(y){}
};
int w, h;
int ans;
char map[25][25];
bool reach[25][25] = { false };
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
int main()
{
while (cin >> w >> h)
{
if (w == 0 && h == 0)
break;
queue<point> q;
ans = 0;
for (int i = 0; i < 25; ++i)
for (int j = 0; j < 25; ++j)
reach[i][j] = false;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
{
cin >> map[i][j];
if (map[i][j] == '@')
{
map[i][j] = '.';
reach[i][j] = true;
ans++;
q.push(point(i, j));
}
if (map[i][j] == '#')
{
reach[i][j] = true;
}
}
while (!q.empty())
{
point tmp = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if (xx > 0 && xx <= h && yy > 0 && yy <= w && !reach[xx][yy])
{
reach[xx][yy] = true;
ans++;
q.push(point(xx, yy));
}
}
}
cout << ans << endl;
}
return 0;
}
相关文章推荐
- HTML标准属性
- 羽毛球比赛中羽毛球擦网和卡网(挂网)怎么判罚?
- 多线程编程(一)多线程基础
- mybatis_user_guide(6) Java API
- maven详解五、maven mvn 命令行的操作和使用
- Ojective-C入门请读
- 基础算法学习-求组合数
- iOS_逆向_使用class dump导出头文件
- 字节流调试笔记
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性