UVa 11110 - Equidivisions
题目大意:给一个n*n的矩阵,其中放置n个数字,判断四连通的相同数字的个数是否等于n。
Flood fill,本来没什么,用dfs判断一下就可以了,可是用scanf读取输入时TLE了,然后看到别人说要用gets()读一整行,因为每行不一定是n对数,好吧,怎么感觉有点坑呢...把字符串解析成数字有没有什么好办法?...
#include <cstdio>
#include <cctype>
#include <cstring>
#define MAXN 110 int mat[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, cnt;
const int dir[][] = {{-, }, {, -}, {, }, {, }}; void dfs(int x, int y)
{
vis[x][y] = ;
cnt++;
for (int i = ; i < ; i++)
{
int dx = x + dir[i][];
int dy = y + dir[i][];
if (dx >= && dx < n && dy >= && dy < n && mat[dx][dy] == mat[x][y] && !vis[dx][dy])
dfs(dx, dy);
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int x, y;
char str[];
while (scanf("%d", &n) && n)
{
getchar();
memset(mat, , sizeof(mat));
for (int i = ; i < n; i++)
{
gets(str);
int len = strlen(str);
for (int j = ; j < len; )
{
while (j < len && !isdigit(str[j])) j++;
if (j == len) break;
x = str[j] - '';
j++;
while (j < len && isdigit(str[j]))
{
x = x * + str[j] - '';
j++;
}
while (j < len && !isdigit(str[j])) j++;
y = str[j] - '';
j++;
while (j < len && isdigit(str[j]))
{
y = y * + str[j] - '';
j++;
}
mat[x-][y-] = i;
}
}
memset(vis, , sizeof(vis));
bool ok = true;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
if (!vis[i][j])
{
cnt = ;
dfs(i, j);
if (cnt != n)
{
ok = false;
goto s;
}
}
s: if (ok) printf("good\n");
else printf("wrong\n");
}
return ;
}
UVa 11110 - Equidivisions的更多相关文章
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
随机推荐
- Java8学习笔记----Lambda表达式 (转)
Java8学习笔记----Lambda表达式 天锦 2014-03-24 16:43:30 发表于:ATA之家 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人 ...
- android 在代码中使用 #ffffff 模式 设置背景色
differentsum.setBackgroundColor(Color.parseColor("#F3733F"));
- ubuntu12.04 android studio 安装
ubuntu12.04 android studio 安装 分类: android 2014-02-17 15:57 10756人阅读 评论(0) 收藏 举报 1.下载JDK ,我下载的是jdk-7u ...
- L4,an exciting trip
expressions: a great number of 许多 in the centre of 在…的中部 sentences: I have just had breakfast. I hav ...
- PD生成oracle表名带引号解决方案
使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会 严格按照“”中的名称建表,如果没有“”,会按照ORAC ...
- Developing Backbone.js Applications
https://addyosmani.com/backbone-fundamentals/
- android:windowSoftInputMode属性使用 软键盘
android:windowSoftInputMode="adjustResize|stateHidden" windowSoftInputMode属性设置值说明. <act ...
- string 与wstring 的转换
std::wstring StringToWString(const std::string &str) { std::wstring wstr(str.length(),L' '); std ...
- 怎样让外界无法改变自定义view的尺寸大小
重写setFrame和setBounds方法即可. + (instancetype)testView { return [[self alloc] init]; } - (void)setFrame: ...
- Quartz 2D 初步
转载自:http://www.cofcool.net/development/2015/06/17/ios-study-note-six-Quartz2D/ Quartz 2D是一个二维绘图引擎,同时 ...