UVa11520 Fill the Square (字典序枚举)

链接:http://vjudge.net/problem/18268

分析:从上到下从左到右按字典序从小到大枚举。

 1 #include <cstdio>
 2
 3 const int maxn = 10 + 5;
 4
 5 int n;
 6 char grid[maxn][maxn];
 7
 8 int main() {
 9     int T;
10     scanf("%d", &T);
11     for (int kase = 1; kase <= T; kase++) {
12         scanf("%d", &n);
13         for (int i = 0; i < n; i++) scanf("%s", grid[i]);
14         for (int i = 0; i < n; i++)
15             for (int j = 0; j < n; j++) if (grid[i][j] == ‘.‘)
16                 for (char ch = ‘A‘; ch <= ‘Z‘; ch++) {
17                     if (i > 0 && grid[i - 1][j] == ch) continue;
18                     if (i < n - 1 && grid[i + 1][j] == ch) continue;
19                     if (j > 0 && grid[i][j - 1] == ch) continue;
20                     if (j < n - 1 && grid[i][j + 1] == ch) continue;
21                     grid[i][j] = ch; break;
22                 }
23         printf("Case %d:\n", kase);
24         for (int i = 0; i < n; i++) printf("%s\n", grid[i]);
25     }
26     return 0;
27 }
时间: 2024-08-02 11:32:10

UVa11520 Fill the Square (字典序枚举)的相关文章

uva 11520 Fill the Square(枚举)

uva 11520 Fill the Square In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty blocks and others already filled for you with some letters to make your task e

[2016-03-19][UVA][11520][Fill the Square]

时间:2016-03-19 14:52:10 星期六 题目编号:[2016-03-19][UVA][11520][Fill the Square] 题目大意:给定n<=10 的不完全矩阵,求填充完整矩阵,使得相邻的字母不同,并且字典序最小,输出最终的矩阵 方法:从左到右,从上到下,每个空格枚举'A'-'Z'的所有情况,满足就跳出 #include <cstdio> using namespace std; #define FOR(x,y,z) for(int (x)=(y);(x)<

UVa 11520 - Fill the Square

题目:给你一个n*n的格子,有些里面有大写字母,用大写字母填满格子,相邻的格子中字母不相同, 并且使得从上到下,从左到右的字母字典序最小. 分析:构造.将格子从上到下,从左到右编号,然后按编号填充,避免冲突即可,这样一定最小. (如果,该方案不是最小,那么之前一定会选择更小的方案,而不是本方案) 说明:╮(╯▽╰)╭. #include <cstdlib> #include <cstring> #include <cstdio> char maps[12][12]; i

UVA - 11520 - Fill the Square(贪心)

题意:给定一个n * n(1 <= n <= 10)的网格,有些已经填上了一些大写字母,需要补充完所有的字母,使每两两相邻的格子中的字母不同,且从上至下,从左至右,组成一个字符串后字典序最小. 由于组成字符串后的长度都为n * n,故字典序越往前的字母决定的优先级越大,所以贪心即可,从上到下,从左到右的补充格子里的字母,尽可能的使当前可以补充的字母尽量小. #include<cstdio> #include<cstring> #include<cctype>

Uva 11520 - Fill the Square 贪心 难度: 0

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515 题意 n*n矩阵中填入大写字母,n <= 10,要求从上到下从左到右字母序最小,相邻格子字母不同 思路 如刘书思路,状态比较小,不会导致矛盾. 感想 1. 状态较小 代码 #include <algorithm> #include <cassert>

UVA11470 Square Sums【水题】

Do you know that there are squares within a square. This might seem confusing, but take a look at this. ????Suppose you have a square grid of size 5 × 5 completely filled with integers. ????You can make three squares from the table above... well ther

贪心思维 专题记录 2017-7-21

A.UVa 10382 - Watering Grass 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数. 思路 :转化一下 将二维降成一维      d = sqrt(1.0*r*r-w*w/4.0) 接着就是区间覆盖问题了 #include <bits/stdc++.h> using namespace std; const int maxn = 10000+10;

poj 2488 A Knight&#39;s Journey (DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34660   Accepted: 11827 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

思维专题(不定期更新)

1.UVa 11100 - The Trip, 2007 题意:给出若干大小不同的包裹,小的能够装在大的包裹里面.求最小的大包裹数,并且保证在所有的大包裹中,所含有的小包裹数目最小. 思路:显然,相同大小的包只能放在不同的大包里,那么最小的大包数目就是相同大小的包的最大数目,记为k.之后,根据从小到大排序后,对于每个大包i可选取从i开始,间隔k个包的那些包裹作为该大包从里到外的各个包裹. 1 #include<iostream> 2 #include<algorithm> 3 us