K - Black And White(涂颜色)

In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color. 
— Wikipedia, the free encyclopedia 

In this problem, you have to solve the 4-color problem. Hey, I’m just joking. 

You are asked to solve a similar problem: 

Color an N × M chessboard with K colors numbered from 1 to K such that no two adjacent cells have the same color (two cells are adjacent if they share an edge). The i-th color should be used in exactly c i cells. 

Matt hopes you can tell him a possible coloring.

InputThe first line contains only one integer T (1 ≤ T ≤ 5000), which indicates the number of test cases. 

For each test case, the first line contains three integers: N, M, K (0 < N, M ≤ 5, 0 < K ≤ N × M ). 

The second line contains K integers c i (c i > 0), denoting the number of cells where the i-th color should be used. 

It’s guaranteed that c 1 + c 2 + · · · + c K = N × M . 
OutputFor each test case, the first line contains “Case #x:”, where x is the case number (starting from 1). 

In the second line, output “NO” if there is no coloring satisfying the requirements. Otherwise, output “YES” in one line. Each of the following N lines contains M numbers seperated by single whitespace, denoting the color of the cells. 

If there are multiple solutions, output any of them.Sample Input

4
1 5 2
4 1
3 3 4
1 2 2 4
2 3 3
2 2 2
3 2 3
2 2 2

Sample Output

Case #1:
NO
Case #2:
YES
4 3 4
2 1 2
4 3 4
Case #3:
YES
1 2 3
2 3 1
Case #4:
YES
1 2
2 3

解法:
 1 #include <iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 
 5 using namespace std;
 6 
 7 const int MAX = 30 +19;
 8 int Map[MAX][MAX],a[MAX];
 9 int L,W,n;
10 int pand(int x,int y,int i)
11 {
12     if(Map[x-1][y]==i) return 0;
13     if(Map[x][y-1]==i) return 0;
14     return 1;
15 }
16 int DFS(int x,int y)
17 {
18     if(x>L)
19     return 1;
20 
21     for(int i=1;i<=n;i++)
22     if(((L-x)*W+W-y+2)/2<a[i])
23     return 0;
24 
25     for(int i=1;i<=n;i++)
26     {
27         int TX=0;
28         if(a[i]&&pand(x,y,i)){
29             Map[x][y]=i;
30             a[i]--;
31             if(y==W)
32             TX = DFS(x+1,1);
33             else
34              TX = DFS(x,y+1);
35             a[i]++;
36         }
37         if(TX)
38             return 1;
39     }
40     return 0;
41 }
42 int main()
43 {
44     int N0=0,N;
45     cin>>N;
46     while(N--)
47     {
48         memset(a,0,sizeof(a));
49         memset(Map,0,sizeof(Map));
50 
51         cin>>L>>W>>n;
52         for(int i=1;i<=n;i++)
53             cin>>a[i];
54             cout<<"Case #"<<++N0<<":"<<endl;
55 
56             if(DFS(1,1))
57             {
58                 cout<<"YES"<<endl;
59                 for(int i=1;i<=L;i++)
60                 {
61                     cout<<Map[i][1];
62                     for(int j=2;j<=W;j++)
63                     {
64                         cout<<" "<<Map[i][j];
65                     }
66                     cout<<endl;
67                 }
68             }
69             else
70                     cout<<"NO"<<endl;
71     }
72     return 0;
73 }

 

转载于:https://www.cnblogs.com/a2985812043/p/7241838.html

假如你是Python老师以下是我的答辩作业,你会问我哪些问题并给出答案import pygame import random # 游戏窗口大小 SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 初始化游戏 pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Challenging Game") clock = pygame.time.Clock() # 创建玩家矩形 player_rect = pygame.Rect(0, 0, 50, 50) player_rect.centerx = SCREEN_WIDTH // 2 player_rect.centery = SCREEN_HEIGHT // 2 player_speed = 5 # 创建敌人列表 enemies = [] enemy_size = 30 enemy_speed = 2 for _ in range(10): enemy_rect = pygame.Rect(0, 0, enemy_size, enemy_size) enemy_rect.x = random.randint(0, SCREEN_WIDTH - enemy_rect.width) enemy_rect.y = random.randint(0, SCREEN_HEIGHT - enemy_rect.height) enemies.append(enemy_rect) # 创建目标对象 target_rect = pygame.Rect(0, 0, 20, 20) target_rect.x = random.randint(0, SCREEN_WIDTH - target_rect.width) target_rect.y = random.randint(0, SCREEN_HEIGHT - target_rect.height) # 游戏主循环 running = True score = 0 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_rect.left > 0: player_rect.x -= player_speed if keys[pygame.K_RIGHT] and player_rect.right < SCREEN_WIDTH: player_rect.x += player_speed if keys[pygame.K_UP] and player_rect.top > 0: player_rect.y -= player_speed if keys[pygame.K_DOWN] and player_rect.bottom < SCREEN_HEIGHT: player_rect.y += player_speed # 更新敌人位置 for enemy_rect in enemies: enemy_rect.x += random.randint(-enemy_speed, enemy_speed) enemy_rect.y += random.randint(-enemy_speed, enemy_speed) # 检测玩家与敌人的碰撞 for enemy_rect in enemies: if player_rect.colliderect(enemy_rect): running = False # 检测玩家与目标的碰撞 if player_rect.colliderect(target_rect): score += 1 target_rect.x = random.randint(0, SCREEN_WIDTH - target_rect.width) target_rect.y = random.randint(0, SCREEN_HEIGHT - tar
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值