码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-16 20:43:50      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:题意   class   eof   ret   stdin   iostream   ios   play   tput   

题目

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>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<double, int> MyPair;
const int MAXN = 11;
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
char maze[MAXN][MAXN];
bool used[26];
void fillin(int i, int j, int n) {
    memset(used, 0, sizeof used);
    for (int di = 0; di < 4; di++) {
        int tx = i + dx[di];
        int ty = j + dy[di];
        if (0 > tx || tx >= n)continue;
        if (0 > ty || ty >= n)continue;
        if (maze[tx][ty] == .)continue;
        used[maze[tx][ty] - A] = true;
    }
}


int main() {
#ifdef LOCAL_DEBUG
    freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
    //freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int T;
    scanf("%d", &T);
    for (int ti = 1; ti <= T; ti++) {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)scanf("%s", maze + i);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (maze[i][j] != .)continue;
                fillin(i, j, n);
                for (char c = A; c <= Z; c++) {
                    if (!used[c - A]) {
                        maze[i][j] = c;
                        break;
                    }
                }
            }
        }
        printf("Case %d:\n", ti);
        for (int i = 0; i < n; i++)printf("%s\n", maze[i]);
        
    }

    return 0;
}
View Code

 

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

标签:题意   class   eof   ret   stdin   iostream   ios   play   tput   

原文地址:https://www.cnblogs.com/xuesu/p/10389111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!