1.题目描述:点击打开链接
2.解题思路:本题是四色定理的模板题,不过有几种情况要提前特判一下:n==1直接输出,1<n<5时候无解,n==6时候套用模板会出现相同的块,因此要特判一下,其他情况都能直接利用模板构造出来。
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cassert> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<cctype> #include<functional> using namespace std; #define me(s) memset(s,0,sizeof(s)) #define pb push_back typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> P; const int N = 117; char mm[N][N]; char col[2]; int n; void solve() //四色定理的模板 { memset(mm, '.', sizeof(mm)); for (int i = 0; i<n; i++) for (int j = 0; j<n; j++) mm[i][j] = 'B'; for (int i = 0; i<n; i++) mm[0][i] = 'Y'; for (int i = 0; i<(n - 1) / 2; i++) { char c = col[i % 2]; for (int j = i + 1; j<n; j++) mm[j][i] = c; for (int j = 1; j <= i + 1; j++) mm[j][i + 1] = c; } for (int i = (n - 1) / 2; i<n - 2; i++) { char c = col[i % 2]; for (int j = i + 2; j<n; j++) mm[j][i] = c; mm[i + 2][i + 1] = c; for (int j = 2; j <= i + 2; j++) mm[j][i + 2] = c; } for (int i = 0; i<n; i++) { for (int j = 0; j<n; j++) printf("%c", mm[i][j]); printf("\n"); } } int main() { col[0] = 'G'; col[1] = 'R'; int T; scanf("%d", &T); while (T--) { scanf("%d", &n); if (n == 1)printf("Y\n"); else if (n <= 4) printf("No solution!\n"); else if (n == 6) { printf("YYYYYY\n"); printf("GGRGRR\n"); printf("GRRGRB\n"); printf("GRGGRB\n"); printf("GRGRRB\n"); printf("GRGBBB\n"); } else solve(); //n==5和n>6时候可以直接使用模板构造 } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)
原文地址:http://blog.csdn.net/u014800748/article/details/47749247