UVa11520-Fill the Square
来源:互联网 发布:淘宝平板客户端和手机 编辑:程序博客网 时间:2024/06/05 19:25
给定一个n*n的方阵要求往里面填充字符使得字典序最小。
暴力搜索,从上到下从左至右选择最小的字符填充即可。
#include <cstdio>char grid[12][12];int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};int n;void Dfs(int x, int y) { if (y >= n) { Dfs(x + 1, 0); return; } if (x > n - 1) { return; } if (grid[x][y] != '.') { Dfs(x, y + 1); return; } for (int i = 0; i < 26; i++) { char ch = 'A' + i; bool ok = true; for (int j = 0; j < 4; j++) { int nx = x + dx[j]; int ny = y + dy[j]; if (nx >= 0 && nx < n && ny >= 0 && ny < n) { if (grid[nx][ny] == ch) { ok = false; break; } } } if (ok) { grid[x][y] = ch; Dfs(x, y + 1); break; } }}int main(int argc, char const *argv[]) { int T; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d", &n); getchar(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%c", &grid[i][j]); } getchar(); } Dfs(0, 0); printf("Case %d:\n", t); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%c", grid[i][j]); } putchar('\n'); } } return 0;}
0 0
- uva11520 Fill the Square
- uva11520 Fill the Square
- Uva11520 - Fill the Square
- uva11520 - Fill the Square
- uva11520 Fill the Square
- UVa11520-Fill the Square
- uva11520 Fill the Square
- 【贪心】【uva11520】 Fill the Square
- UVA11520(p31)----Fill the Square
- UVA11520 Fill the Square 枚举
- UVA11520 Fill the Square 枚举
- 11520 - Fill the Square
- 11520 - Fill the Square
- Fill the Square
- Fill the Square
- 11520 - Fill the Square
- Fill the Square
- Fill the Square UVA
- Android 拦截 LinearLayout RelativeLayout ViewGroup 监听 传递到下面布局
- VR 触摸板模拟上下左右按键
- CodeForces 718A
- Uh, First, You know what I will say as a coder
- jquery选择器 之 获取父级元素、同级元素、子元素
- UVa11520-Fill the Square
- hdu 2063 过山车【二分图匹配,ver匈牙利
- Hawq学习笔记 --- How to access HDFS data via GPDB external table with gphdfs protocol
- Codis 使用
- Android Studio该如何创建虚拟机呢?包括错误无法启动虚拟机
- 立体视觉:算法和应用(七)
- C Primer Plus学习 二
- Docke 1.12 基础篇:48条命令(6)
- 第5周项目2-建立链栈算法库