ural 1250

2024-06-10 17:32
文章标签 ural 1250

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048808

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

2024年AMC10美国数学竞赛倒计时两个月:吃透1250道真题和知识点(持续)

根据通知,2024年AMC10美国数学竞赛的报名还有两周,正式比赛还有两个月就要开始了。计划参赛的孩子们要记好时间,认真备考,最后冲刺再提高成绩。 那么如何备考2024年AMC10美国数学竞赛呢?做真题,吃透真题和背后的知识点是备考AMC8、AMC10有效的方法之一。通过做真题,可以帮助孩子找到真实竞赛的感觉,而且更加贴近比赛的内容,可以通过真题查漏补缺,更有针对性的补齐知识的短板。

后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

1297. Palindrome Time Limit: 1.0 second Memory Limit: 16 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlim

【URAL】1057 Amount of Degrees 数位DP

传送门:【URAL】1057 Amount of Degrees 题目分析:将数转化成能达到的最大的01串,串上从右往左第i位为1表示该数包括B^i。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>using namespace std ;typedef long long LL ;#de

ural Minimal Coverage (区间覆盖)

http://acm.timus.ru/problem.aspx?space=1&num=1303 给出一些区间,选择尽量少的区间能覆盖到[0,m]。 小白p154,典型的区间覆盖问题。一直在想怎么dp。。 首先预处理,先按左端点从小到大排序,若左端点相同右端点从大到小排序,若区间x完全包含y,按照贪心的思想,y是没有意义的,有大区间可以选何必选择小区间。处理完事之后各个区间满足a1