ural 1250
有点坑的dfs 看懂题应该就会做了 神圣海必然围成一个圈 dfs将神圣还外围的全部去掉 简单题
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
int dx[] = {1, 1, 1, -1, -1, -1, 0, 0};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int ex[] = {1, -1, 0, 0};
int ey[] = {0, 0, 1, -1};
int g[510][510],w,h;
bool ok(int x, int y)
{
if(x >= 1 && y >= 1 && x <= h && y <= w)
return true;
return false;
}
void dfs1(int x, int y)
{
if(g[x][y] == 1)
{
g[x][y] = 2;
for(int i = 0; i < 8; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(ok(nx,ny))
dfs1(nx, ny);
}
}
}
void dfs2(int x, int y)
{
if(g[x][y] != 2 && g[x][y] != 3)
{
g[x][y] = 3;
for(int i = 0; i < 4; i++)
{
int nx = x+ex[i], ny = y+ey[i];
if(ok(nx,ny))
dfs2(nx, ny);
}
}
}
void dfs3(int x, int y)
{
if(g[x][y] == 0)
{
g[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int nx = x+ex[i], ny = y+ey[i];
if(ok(nx,ny))
dfs3(nx, ny);
}
}
}
int main()
{
int x,y;
char c;
scanf("%d%d%d%d",&w,&h,&x,&y);
getchar();
memset(g, 0, sizeof(g));
for(int i = 1; i <= h; i++)
{
for(int j = 1; j <= w; j++)
{
scanf("%c",&c);
if(c == '.')
g[i][j] = 1;
}
getchar();
}
dfs1(y, x);
for(int i = 1; i <= w; i++)
{
if(g[1][i] != 2)
dfs2(1, i);
if(g[h][i] != 2)
dfs2(h, i);
}
for(int i = 1; i <= h; i++)
{
if(g[i][1] != 2)
dfs2(i, 1);
if(g[i][w] != 2)
dfs2(i, w);
}
int ans = 0;
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++)
{
if(!g[i][j])
{
dfs3(i, j);
ans++;
}
}
printf("%d\n",ans);
return 0;
}
ural 1250的更多相关文章
- Ural 1250 Sea Burial 题解
目录 Ural 1250 Sea Burial 题解 题意 输入 题解 程序 Ural 1250 Sea Burial 题解 题意 给定一个\(n\times m\)的地图,\(.\)为水,\(\#\ ...
- ural 1250. Sea Burial
1250. Sea Burial Time limit: 1.0 secondMemory limit: 64 MB There is Archipelago in the middle of a s ...
- URAL 1250 Sea Burial 简单Floodfill
问这个人掉落的海域包含几个岛屿. 八方向相连为同一片海域,四方向相连为同一个岛屿.与边界相连的岛屿不算. 方法:在给定地图外面填充一圈".",从这个人掉落的地方开始进行floodf ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- ural 2068. Game of Nuts
2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...
随机推荐
- 对JavaScript莫名的愤怒
很多时候,我都察觉JavaScript有一中描述性语言的特性,为了实现他的功能,在浏览器中完美的发挥作用,他就像是一个巨型的工厂工程模式,外挂了很多API和功能集,他们试图用完美的方案去解释所有必须的 ...
- 2014-10 u-boot make xxx_defconfig 过程分析
/** ****************************************************************************** * @author Maox ...
- Dao层和Service层设计
1.Dao接口层 public interface IBaseDao<T, ID extends Serializable>{ public abstract Serializable s ...
- C#语言的Image和byte数组的互相转换
/// <summary> /// 字节数组转换为图片 /// </summary> /// <param name="buffer">字节数组 ...
- Android核心组件 Service
Service: 服务 Service 是Activity系统的核心组件之一. 因此需要继承和注册 Service 是内有界面的, 适合在后台长期的执行任务. (如放歌, 检测版本跟新, 下载, 上传 ...
- php代码加密|PHP源码加密——实现方法
Encipher - PHP代码加密 | PHP源码加密下载地址:https://github.com/uniqid/encipher 该加密程序是用PHP代码写的,加密后代码无需任何附加扩展,无需安 ...
- laravel扩展Debugbar
github地址:https://github.com/barryvdh/laravel-debugbar
- V9任何页面GET调用内容分页的说明
如标题,很多人想要在网站首页或其他的页面实现分页效果,说明如下: 一般特殊页面实现分页是通过GET语句的(论坛很多牛人用修改PHPCMS系统函数来实现,个人不推荐,因为你改了系统文件,不利于官方下一步 ...
- CLR via C# 异常管理读书笔记
1. 设计异常类型层次结构应该浅而宽 2. 注意使用finally块清理资源 3. 不要什么都捕捉 4.得体地从异常中恢复 5.发生不可恢复的异常时回滚部分完成的操作-维持状态 6.隐藏实现细节来维系 ...
- [Git]代码管理工具简单使用
1 Git简介 Git是分布式的版本控制系统,是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计.与CVS.Subversion一类的集中式版本控 ...