UVa 11520 Fill the Square (水题,暴力)
题意:给n*n的格子里填上A-Z的字符,保证相邻字符不同,并且字典序最小。
析:直接从第一个格子开始暴力即可,每次判断上下左是不是相同即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[15][15];
bool ok; bool dfs(int r, int c){
if(isalpha(s[r][c])){
if(r == n-1 && c == n-1) return true;
else if(c == n-1) return dfs(r+1, 0);
else return dfs(r, c+1);
}
for(int i = 0; i < 26; ++i){
if(r > 0 && s[r-1][c] == 'A' + i) continue;
if(c > 0 && s[r][c-1] == 'A' + i) continue;
if(r < n-1 && s[r+1][c] == 'A' + i) continue;
if(c < n-1 && s[r][c+1] == 'A' + i) continue;
s[r][c] = 'A' + i;
if(r == n-1 && c == n-1) return true;
else if(c == n-1) return dfs(r+1, 0);
else return dfs(r, c+1);
}
return true;
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%s", s+i);
ok = false;
dfs(0, 0);
printf("Case %d:\n", kase);
for(int i = 0; i < n; ++i) puts(s[i]);
}
return 0;
}
UVa 11520 Fill the Square (水题,暴力)的更多相关文章
- uva 11520 - Fill the Square
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11520 Fill the Square 填充正方形
在一个 n * n 网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如果有多重填法,则要求按照从上到下,从左到右的顺序把所有格子连 ...
- UVA 11520 Fill the Square(模拟)
题目链接:https://vjudge.net/problem/UVA-11520 这道题我们发现$n\leq 10$,所以直接进行暴力枚举. 因为根据字典序所以每个位置试一下即可,这样的复杂度不过也 ...
- Uva 11520 - Fill the Square 贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11636 Hello World! (水题思维)
题意:给你一个数,让你求需要复制粘贴多少次才能达到这个数. 析:这真是一个水题,相当水,很容易知道每次都翻倍,只要大于等于给定的数就ok了. 代码如下: #include <iostream&g ...
- UVa 11520 Fill in the Square
题意:给出 n*n的格子,把剩下的格子填上大写字母,使得任意两个相邻的格子的字母不同,且从上到下,从左到右的字典序最小 从A到Z枚举每个格子填哪一个字母,再判断是否合法 #include<ios ...
- uva 489 Hangman Judge(水题)
题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题
B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
随机推荐
- 你必须了解的java内存管理机制(一)-运行时数据区
前言 本打算花一篇文章来聊聊JVM内存管理机制,结果发现越扯越多,于是分了四遍文章(文章讲解JVM以Hotspot虚拟机为例,jdk版本为1.8),本文为其中第一篇.from 你必须了解的java内存 ...
- Ubuntu引导出问题grub rescu模式下:“error : unknown filesystem”或者 找不到normal.mod 的解决办法
感谢http://www.linuxidc.com/Linux/2012-06/61983.htm,因为参考了其中的内容. 下面是修改和完善. 问题原因: (win7,ubuntu双系统下) 强制关机 ...
- 说说设计模式~单件模式(Singleton)
单件模式(Singleton)要求一个类有且仅有一个实例,并且提供了一个全局的访问点. 从概念上来研究一下它的实现,不考虑线程安全 1 public sealed class Singlton 2 { ...
- 2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本
密码设置时要注意mysql版本.版本不同,效果不同. 方法/步骤 安装好wamp后,右击wamp->MySQl->MySql console(控制台) 提示输入密码,因为密码为 ...
- EasyCamera海康摄像机向EasyDarwin云平台推送音视频数据的缓存设计
本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376 EasyCamera在向EasyDarwin云平台推送音视频数据时,有时一个I帧会很 ...
- Markov and Chebyshev Inequalities and the Weak Law of Large Numbers
https://www.math.wustl.edu/~russw/f10.math493/chebyshev.pdf http://www.tkiryl.com/Probability/Chapte ...
- vim实现代码缩进和可视区域的字符串替换
今天2014年9月12号,实现了vim下的代码自动缩进和替换可视区域的字符串,之前一直在用vim这个强大的编辑器,它的强大只有用了的人才知道,现在把这两个很强大的功能展示出来,有个这两个功能,即使你写 ...
- CSS阶段总结
CSS布局之左右布局与左中右布局 方法:为子元素设置浮动,然后在其父元素上使用clearfix类来清除浮动.代码示例: html部分: <div class="parent clear ...
- cookie VS sessionstorge VS localstorge
虽然cookie , localstorge , sessionstorge三者都有存储的功能,但是还是有区别, git上地址:https://github.com/lily1010/cookie-s ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...