uva 11520 Fill the Square

点击打开链接uva 11520

思路:dfs
分析:
1 题目给定一个n*n的地图,这个地图上面是一些空格和大写字母,现在要求把这个地图填满并且使得这个地图有最小的字典序
2 很明显的搜索题,我们只要通过枚举这个地图找到一个空格就进行dfs填充,最后得到的肯定是最小的字典序

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 15;
char map[N][N];
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

//判断当前字母是否满足
bool ok(int x , int y , int n , char ch){
    bool mark = true;
    for(int i = 0 ; i < 4 ; i++){
       int tmpx = x+dir[i][0];
       int tmpy = y+dir[i][1];
       if(tmpx >= 0 && tmpy < n && map[tmpx][tmpy] == ch)
         mark = false;
    }
    return mark;
}

void dfs(int x , int y , int n){
    if(map[x][y] != '.')
       return;
    for(int i = 65 ; i < 65+26 ; i++){
       if(ok(x , y , n , i)){
          map[x][y] = i;
          break;
       }
    }
    for(int i = 0 ; i < 4 ; i++){
       int tmpx = x+dir[i][0];
       int tmpy = y+dir[i][1];
       if(tmpx >= 0 && tmpy < n)
         dfs(tmpx , tmpy , n);
    }
}

void solve(int n){
    for(int i = 0 ; i < n ; i++){
       for(int j = 0 ; j < n ; j++){
          if(map[i][j] == '.')
             dfs(i , j , n);
       }
    }
    for(int i = 0 ; i < n ; i++){
       for(int j = 0 ; j < n ; j++)
          printf("%c" , map[i][j]);

       printf("\n");
    }
}

int main(){
    int  n , T , Case = 1;
    scanf("%d" , &T);
    while(T--){
       scanf("%d%*c" , &n);
       for(int i = 0 ; i < n ; i++)
           gets(map[i]);
       printf("Case %d:\n" , Case++);
       solve(n);
    }
    return 0;
}
时间: 2024-08-30 09:09:07

uva 11520 Fill the Square的相关文章

UVA之Fill the Square

Problem A Fill the Square Input: Standard Input Output: Standard Output   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 fill

uva 10603 - Fill bfs

10603 - Fill         经典的倒水问题,求最接近的可得到的水量d' d'<=d 和需要倒的水量       因为a,b,c最大只有200,所以总的状态只有8,000,000,而又因为总水量恒定,所以状态量只有40,000,bfs遍历即可.   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #i

uva 10603 - Fill

点击打开链接 题目所属:  隐式图的搜索 题目意思:  有三个容器大小为 a b  c 刚开始 a b都是空的 c是满的,现在给定一个d 大小的水量 ,要求我么找到最小的倒水总量使得有一个容器的水量为 d,如果找不到那么就用d'代替d(d' < d)直到能够找到有一个容器的水量为 d为止,最后输出 最小的倒水总量和 d' 解题思路:   倒水条件:假设是a倒入b中,那么必须要有a被倒空或 b 被倒满.                     经典的倒水问题,只是结果是要我们输出最小的倒水总量而不

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10023 Square root:高精度及开平方公式

10023 - Square root Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=964 The Problem You are to determinate X by given Y, from expression The Input The first line is the n

UVa 10603:Fill,经典倒水问题+隐式图搜索+dfs

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544 类型: 隐式图搜索 原题: There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater th

UVA之409 - Excuses, Excuses!

 Excuses, Excuses!  Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write

UVa 108:Maximum Sum

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=44 原题: Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dim

UVa 10827:Maximum sum on a torus

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1768 原题: A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an inte