RC-v6 拼瓷砖
分数 20
全屏浏览
切换布局
作者 陈越
单位 浙江大学
对照设计师给出的瓷砖拼接图案,请你统计一下需要多少种不同的瓷砖各多少块?
这里每块瓷砖都是单一颜色的正方形,每种颜色用一个 {
0
-9
,a
-z
,A
-Z
} 集合中的字符来表示。当设计图中有一方块颜色的面积为 L×L 时,我们将用一整块边长为 L 的正方形瓷砖来填充,而不会选用较小的同色瓷砖来拼接。此外,为了避免多解的情况,我们规定必须按照从上到下、从左到右的顺序贴瓷砖(参见样例解释),瓷砖不可重叠,并且要求每一步选用的瓷砖的面积尽可能大。输入格式:
输入首先在第一行中给出两个不超过 103 的正整数 N 和 M,对应整面墙的高和宽。随后 N 行,每行给出 M 个字符,对应这一行的颜色分布。
输出格式:
首先在第一行输出不同瓷砖的种类数 K。随后 K 行,每行按格式
color = C; size = L; amount = T
输出一种瓷砖的信息。其中
C
是表示颜色的字符,L
是正方形的边长,T
是这种瓷砖需要的数量。瓷砖按照其颜色的升序输出,同色的瓷砖按照其边长的升序输出。
输入样例:
6 6 aaadee aacbee deccda caccbe ddecbb ddadbb
输出样例:
10 color = a; size = 1; amount = 4 color = a; size = 2; amount = 1 color = b; size = 1; amount = 2 color = b; size = 2; amount = 1 color = c; size = 1; amount = 3 color = c; size = 2; amount = 1 color = d; size = 1; amount = 4 color = d; size = 2; amount = 1 color = e; size = 1; amount = 3 color = e; size = 2; amount = 1
样例解释:
下图中的数字给出了贴瓷砖的顺序。
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
思路:直接二分,无脑check,然后标记,真是服了,不解释,还有一种方法,我自己用的bfs不知道为什么有6分没过, 希望有大佬可以解答一下
二分满分
#include "bits/stdc++.h" using namespace std; typedef pair<char, int> PI; const int N = 1000 + 10; map<PI, int> mp; char g[N][N]; int vis[N][N]; bool check(int x, int y, int len){ for(int i = x; i < x + len; i ++){ for(int j = y; j < y + len; j ++){ if(g[i][j] != g[x][y] || vis[i][j]) return false; } } return true; } void init(int x, int y, int len){ for(int i = x; i < x + len; i ++){ for(int j = y; j < y + len; j ++){ vis[i][j] = 1; } } } int main(){ int n, m; cin>>n>>m; char a; for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ cin>>g[i][j]; } } for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ if(!vis[i][j]){ int l = 1, r = min((n - i), (m - j)); int mid; while(l < r){ mid = l + r + 1>>1; if(!check(i, j, mid)){ r = mid - 1; }else { l = mid ; } } init(i,j, l); mp[{g[i][j], l}] ++; } } } cout<<mp.size()<<endl; for(auto x : mp) { auto y = x.first; cout<<"color = " << y.first<<"; size = "<<y.second<<"; amount = "<<x.second<<endl; } return 0; }
bfs 14分
#include "bits/stdc++.h" #include "stdio.h" using namespace std; const int N = 1000 + 10; typedef pair<char, int> PI; map<PI , int> mp; queue<PI> q; char g[N][N]; int vis[N][N]; int dic[3][2] = {0, 1,1, 0, 1, 1}; int n, m; int size = 1; void bfs(int x2, int y2, char a){ while(q.size()) q.pop(); // q.clear(); q.push({x2, y2}); int flag = 0; size = 1; int x, y; while(q.size()){ PI t = q.front(); q.pop(); x = t.first; y = t.second; vis[x][y] = 1; int x1, y1; for(int i = 0; i < 3; i ++){ x1 = x + dic[i][0]; y1 = y + dic[i][1]; if(x1 >= n || y1 >= m || vis[x1][y1] ) continue; if( g[x1][y1] != a) { // cout<<x1<<" "<<y1<<" "<<g[x1][y1]<<" "<<a<<" "; flag = 1; break; } q.push({x1, y1}); } // cout<<flag<<"@@"<<endl; if(flag) break; // cout<<x1<<" "<<y1<<" "<<(x1 - x2)<<" "<<(y1 - y2)<<"&&&"<<endl; if(x1 < n && y1 < m && (x1 - x2) == (y1 - y2)){ for(int i = x2; i <= x1; i ++){ vis[i][y1] = 1; } for(int j = y2; j <= y1; j ++){ vis[x1][j]; } size++; } } } int main(){ cin>>n>>m; for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ cin>>g[i][j]; } } for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j ++){ if(!vis[i][j]) { bfs(i, j, g[i][j]); // cout<<i<<" "<<j<<" "<<g[i][j]<<" "<<size<<" "<<endl; mp[{g[i][j], size}] ++; } }- } // mp[{'a',1}] = 1; cout<<mp.size()<<endl; for(auto x : mp) { auto y = x.first; cout<<"color = " << y.first<<"; size = "<<y.second<<"; amount = "<<x.second<<endl; } }