POJ - 1111 Image Perimeters
来源:互联网 发布:干性皮肤毛孔大 知乎 编辑:程序博客网 时间:2024/05/16 01:29
题意:求'X'围成的周长
思路:按理说每增加一个就是周长加4,但是要减去重复的地方,这里我是用BFS做的,如果是BFS的模板思路的话是不行的,应该要先取出再标记
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 30;struct node {int x,y;};queue<node> qu;int dx[] = {0,0,1,-1,1,1,-1,-1};int dy[] = {1,-1,0,0,1,-1,1,-1};int vis[MAXN][MAXN],R,C,sx,sy,ans;char map[MAXN][MAXN];int check(int x, int y) {if (x >= 0 && x < R && y >= 0 && y < C && map[x][y] == 'X')return 1;return 0;}int bfs(int r, int c) {node st;st.x = r,st.y = c;qu.push(st);while (!qu.empty()) {node cur = qu.front();qu.pop();if (!vis[cur.x][cur.y]) {ans += 4;for (int i = 0; i < 4; i++) {int nx = cur.x + dx[i];int ny = cur.y + dy[i];if (check(nx, ny) && vis[nx][ny])ans -= 2;}vis[cur.x][cur.y] = 1;for (int i = 0; i < 8; i++) {int nx = cur.x + dx[i];int ny = cur.y + dy[i];if (check(nx, ny)) {st.x = nx;st.y = ny;qu.push(st);}}}}return ans;}int main() {while (scanf("%d%d%d%d%*c", &R, &C, &sx, &sy) != EOF && R+C+sx+sy != 0) {memset(map, 0, sizeof(map));memset(vis, 0, sizeof(vis));while (!qu.empty()) qu.pop();ans = 0;for (int i = 0; i < R; i++)gets(map[i]);printf("%d\n", bfs(sx-1, sy-1));}return 0;}
0 0
- POJ 1111 Image Perimeters
- poj 1111 Image Perimeters
- POJ 1111 Image Perimeters
- Poj 1111 Image Perimeters
- POJ 1111 - Image Perimeters
- poj 1111 Image Perimeters
- POJ-1111-Image Perimeters
- poj 1111 Image Perimeters
- POJ 1111 Image Perimeters
- POJ:1111 Image Perimeters
- poj 1111:Image Perimeters
- POJ 1111 Image Perimeters
- POJ Image Perimeters 1111
- poj 1111 Image Perimeters
- POJ - 1111 Image Perimeters
- POJ 1111 Image Perimeters
- POJ 1111 Image Perimeters
- POJ 1111 Image Perimeters
- 推荐引擎内部的秘密
- Java寻找最大连续子串和
- raw_input() 与 input()的区别
- JOptionPane 提示框的一些常用方法
- Excel中的单元格引用
- POJ - 1111 Image Perimeters
- java计算时间差
- android camera(三):camera V4L2 FIMC
- 什么是stub?
- 【水I/O】#20 A. BerOS file system
- hibernate 原理(ORM)及接口
- eclipse调试hbase,mapreduce
- 如何添加、彻底删除Tomcat服务 将解压版的Tomcat设为windows服务
- 值得IT人员浏览的站点