题目链接:https://atcoder.jp/contests/arc080/tasks/arc080_b
Problem Statement
We have a grid with HH rows and WW columns of squares. Snuke is painting these squares in colors 11, 22, ......, NN. Here, the following conditions should be satisfied:
- For each ii (1≤i≤N1≤i≤N), there are exactly aiai squares painted in Color ii. Here, a1+a2+...+aN=HWa1+a2+...+aN=HW.
- For each ii (1≤i≤N1≤i≤N), the squares painted in Color ii are 4-connected. That is, every square painted in Color ii can be reached from every square painted in Color ii by repeatedly traveling to a horizontally or vertically adjacent square painted in Color ii.
Find a way to paint the squares so that the conditions are satisfied. It can be shown that a solution always exists.
Constraints
- 1≤H,W≤1001≤H,W≤100
- 1≤N≤HW1≤N≤HW
- ai≥1ai≥1
- a1+a2+...+aN=HWa1+a2+...+aN=HW
Input
Input is given from Standard Input in the following format:
HH WW NN a1a1 a2a2 ...... aNaN
Output
Print one way to paint the squares that satisfies the conditions. Output in the following format:
c11c11 ...... c1Wc1W :: cH1cH1 ...... cHWcHW
Here, cijcij is the color of the square at the ii-th row from the top and jj-th column from the left.
Sample Input 1 Copy
Copy
2 2 3 2 1 1
Sample Output 1 Copy
Copy
1 1 2 3
Below is an example of an invalid solution:
1 2 3 1
This is because the squares painted in Color 11 are not 4-connected.
Sample Input 2 Copy
Copy
3 5 5 1 2 3 4 5
Sample Output 2 Copy
Copy
1 4 4 4 3 2 5 4 5 3 2 5 5 5 3
Sample Input 3 Copy
Copy
1 1 1 1
Sample Output 3 Copy
Copy
1
题意:
给你一个h*w的网格。给你n中颜色,每种颜色ai个保证所有颜色的和正好=h*w。保证每种颜色都要相连(上,下,左,右)。
找到一种方案:
代码:
#include<bits/stdc++.h>
using namespace std;
int a[10009];
int b[200][200];
int main()
{
int h,w;
int n;
cin>>h>>w>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
int k=1;
for(int i=1; i<=h; i++)
{
if(i%2==1)
{
for(int j=1; j<=w; j++)
{
b[i][j]=k;
a[k]-=1;
if(a[k]==0)
{
k+=1;
}
}
}
else
{
for(int j=w; j>=1; j--)
{
b[i][j]=k;
a[k]-=1;
if(a[k]==0)
{
k+=1;
}
}
}
}
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
}