Codeforces 475C Kamal-ol-molk's Painting 模拟
2014-10-06 11:42
260 查看
题目链接:点击打开链接
题意:给定n*m的矩阵
X代表有色 .代表无色
用一个x*y的矩阵形刷子去涂色。
刷子每次可以→或↓移动任意步。
若能够染出给定的矩阵,则输出最小的刷子的面积
若不能输出-1
思路:
先找到连续最小的x,y
因为至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就可以了。
题意:给定n*m的矩阵
X代表有色 .代表无色
用一个x*y的矩阵形刷子去涂色。
刷子每次可以→或↓移动任意步。
若能够染出给定的矩阵,则输出最小的刷子的面积
若不能输出-1
思路:
先找到连续最小的x,y
因为至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就可以了。
#pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <vector> #include <string.h> using namespace std; #define N 1010 int top; char s ; int n, m, stx, sty; int H(int h, int l, int r){//扫一行 -1表示全是. 1表示全是X 0表示都有 if(h>n || r>m) return -2; int cnt = 0; for(int i = l; i <= r; i++) if(s[h][i]=='X')cnt++; if(cnt==0)return -1; if(cnt==r-l+1)return 1; return 0; } int L(int l, int S, int X){ if(l>m || X>n) return -2; int cnt = 0; for(int i = S; i <= X; i++) if(s[i][l]=='X')cnt++; if(cnt==0)return -1; if(cnt==X-S+1)return 1; return 0; } bool ok(int x, int y){ // printf(" ++++++(%d,%d)\n", x,y); int nowx = stx, nowy = sty; if(nowx + x-1 > n || nowy + y-1>m)return false; for(int i = 0; i < x; i++) for(int j = 0; j < y; j++) if(s[i+nowx][j+nowy]!='X')return false; // puts("---");put(); puts("-----"); int cnt = x*y; while(1){ if(nowx + x <= n && s[nowx+x][nowy] =='X') { nowx ++; for(int i = 0; i < y; i++) if(s[nowx+x-1][i+nowy]!='X') return false; cnt += y; } else if(nowy + y <= m && s[nowx][nowy+y] == 'X') { nowy++; for(int i = 0; i < x; i++) if(s[i+nowx][nowy+y-1]!='X') return false; cnt += x; } else break; // puts("******");put(); } return cnt == top; } int hehe; int feifei; int x, y; void find_xy(){ x = N, y = N; for(int i = 1; i <= m; i++) { int cnt = 0; for(int j = 1; j <= n; j++) { if(s[j][i]=='.') { if(cnt) x = min(x, cnt); cnt = 0; } else cnt++; } if(cnt) x = min(x, cnt); } for(int i = 1; i <= n; i++) { int cnt = 0; for(int j = 1; j <= m; j++) { if(s[i][j]=='.') { if(cnt) y = min(y, cnt); cnt = 0; } else cnt++; } if(cnt) y = min(y, cnt); } } int solve(){ int ans = N*N; for(int i = 1; i <= x; i++) if(ok(i,y)) { ans = i*y; break; } for(int i = 1; i <= y && x*i<ans; i++) if(ok(x,i)) { ans = x*i; break; } if(ans > n*m) return -1; return ans; } void input(){ stx = -1; top = 0; for(int i = 1; i <= n; i++) { scanf("%s", s[i]+1); for(int j = 1; j <= m; j++) { if(stx==-1 && s[i][j]=='X'){ stx = i, sty = j; } top += s[i][j]=='X'; } } } int main(){ while(cin>>n>>m){ input(); find_xy(); printf("%d\n", solve()); } return 0; }
相关文章推荐
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- CodeForces #Bayan# -- C Kamal-ol-molk's Painting 模拟
- Codeforce 475 C. Kamal-ol-molk's Painting
- CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
- 【CODEFORCES】 C. Kamal-ol-molk's Painting
- hdu 4119 Isabella's Message(模拟+字典树)
- ACdream 1196 KIDx's Pagination(模拟)
- ZOJ 3804 YY's Minions(简单模拟)
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
- SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ol
- Python _X __all__ 模拟 导入'私有'变量 及其注意点和实例
- ZOJ 3804 YY's Minions(模拟)
- ZOJ3804:YY's Minions(模拟)
- HDU 4706 Children's Day(模拟啊)
- hdu 4119 Isabella's Message(模拟)
- uva 10881 Piotr's Ants 模拟
- UVa 10205 Stack 'em Up (模拟)
- poj 1828 Monkeys' Pride 模拟
- HDU-#4706 Children's Day(模拟)
- uva_10881_Piotr's Ants (模拟,Sort)