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 ...
随机推荐
- Gson解析数组和list容器
Gson解析数组和list容器 使用Gson解析首先须要增加架包文件:gson-2.2.4.jar 定义一个类Student: public class Student { String name=& ...
- lowB三人组代码示例
冒泡排序:列表在内存重只存一份,所以不需要重复赋值 import random from timewrap import * #时间装饰器 # 初级版本 @cal_time def bubble_so ...
- 调用Camera返回为空的分析及处理方法
前言 大家可能遇到了这种情况.调用Camera,然后指定自己定义的保存路径,结果返回的Intent为空.我们来分析一下原因. 分析 首先看Camera的部分逻辑,在源代码中的Camera.java的d ...
- 解析PE文件的附加数据
解析程序自己的附加数据,将附加数据写入文件里. 主要是解析PE文件头.定位到overlay的地方.写入文件. 常应用的场景是在crackme中,crackme自身有一段加密过的附加数据.在crackm ...
- “checkbox”和“select”对象在javascript和jquery的操作差异做了整理
checkbox checkbox在javascript和jquery中选中和取消的方法 Javascript: document.getElementById("myCheck" ...
- ecmall时间的问题
$time1 = date("Y-m-d H:i:s", gmtime()); $time = date("Y-m-d H:i:s", time()); / ...
- npm ERR! Unexpected end of JSON input while parsing near '...inimist":"^1.2.0"}
简介 在项目中执行npm install安装依赖包的时候.出现npm ERR! Unexpected end of JSON input while parsing near '...inimist& ...
- [IR课程笔记]向量空间模型(Vector Space Model)
VSM思想 把文档表示成R|v|上的向量,从而可以计算文档与文档之间的相似度(根据欧氏距离或者余弦夹角) 那么,如何将文档将文档表示为向量呢? 首先,需要选取基向量/dimensions,基向量须是线 ...
- JavaScript Objects in Detail
JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...
- Python模块:os
OS模块常用用法: os.name() #判断当前使用的系统环境,windows则返回 ‘nt’,Linux则返回‘posix’ os.getcwd() #显示当前目录 os.listdir() #以 ...