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 ...
随机推荐
- android opencv 人脸检测
转载自http://blog.csdn.net/jesse__zhong/article/details/24889709 .......省略包 public class Staticdetectio ...
- Objective-C 【单个对象内存管理(野指针&内存泄露)】
------------------------------------------- 单个对象内存管理 (1)野指针 ①定义了一个指针变量,但是并没有赋初值,它随机指向一个东西 ②某指针变量指向的内 ...
- datatable dateset 载体传递数据、存储过程
第一部分:数据库通过存储过程读取数据,通过datatable接受,前台通过asp:repeater.DataSource()和binding()绑定数据 /// <summary> /// ...
- Bootstrap 标签的变体 实例样式
Bootstrap 标签样式,代码如下: <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 标签的 ...
- 利用HibernateTools从数据库表生成带注解的POJO
在SSH框架中,如果先设计好了数据库,那么下一步就需要从数据库Table生成实体java类和hbm.xml配置文件.在最新的开发框架中,已经支持使用注解,从而避免了繁琐的hbm.xml配置,而且我们可 ...
- input内容改变触发事件,兼容IE
<html> <head> <script type="text/javascript"> window.onload = function() ...
- 将C# dataTable 做为参数传入到存储过程
1.list转换为DataTable(如果有需要) public static DataTable ListToDataTable<T>(List<T> entitys) { ...
- C# login with cookie and fiddler2
http://blog.codeblack.nl/post/HttpWebRequest-HttpWebResponse-and-cookies.aspx CookieContainer cookie ...
- PHP连接局域网MYSQL数据库的简单实例
PHP连接局域网MYSQL数据库的简单实例 [php] view plaincopy <?PHP /** * php连接mysql数据库 * by www.jbxue.com */ $conn= ...
- location跳转和header跳转的区别
1:header("location:url") 跳转之前不能有任何输出,如果想在header之前有输出,则要修改php.ini文件.具体 output_handler =mb_o ...