题目链接:https://cn.vjudge.net/problem/UVA-1605
题意:
用大小写字母代表一个国家,如何给这些国家分配房间才能让他们房间相邻,上下楼层和左右房间四个方向;
思路:
先说说这题我没注意到的坑;第一:每个楼层的规格是相同的,也就是说,每个楼层的长和宽是相等的;第二:这题的W和L,我最初以为W是纵向的,L是横向的,结果题目和我想的相反,而且样例也没有在区别W,L的问题上有明确说明(样例W == L);第三:同个国家的分配的多个房间一定要是相连的,也就是连续的,可以上下连续和左右连续,但是一定不能断开;
我的思路就是分n个楼层,每个楼层给一个国家对应相邻的国家,比如 AA,AB,AC,AD,这样,注意,这里的AA并不是多余的,是为了保证在多个楼层中A是连续的;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0)
typedef long long ll;
const int Maxn = 110;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
char ch[55] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
int main (void)
{
// IOS;
int n;
while (cin >> n) {
cout << n << " " << "2 " << n << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << ch[i] << ch[j] << endl;
}
cout << endl;
}
}
return 0;
}
第二种是紫书上的,差不了多少,这里就不解释了;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0)
typedef long long ll;
const int Maxn = 110;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
char ch[55] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
int main (void)
{
// IOS;
int n;
while (cin >> n) {
cout << "2 " << n << " " << n << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << ch[i];
}
cout << endl;
}
cout << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << ch[j];
}
cout << endl;
}
cout << endl;
}
return 0;
}