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(" "); } } int main() { col[0] = 'G'; col[1] = 'R'; int T; scanf("%d", &T); while (T--) { scanf("%d", &n); if (n == 1)printf("Y "); else if (n <= 4) printf("No solution! "); else if (n == 6) { printf("YYYYYY "); printf("GGRGRR "); printf("GRRGRB "); printf("GRGGRB "); printf("GRGRRB "); printf("GRGBBB "); } else solve(); //n==5和n>6时候能够直接使用模板构造 } }