Codeforce 475 C. Kamal-ol-molk's Painting
从最左上的点開始枚举长宽....
C. Kamal-ol-molk's Painting time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputRumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.
Consider the painting as a n × m rectangular grid. At the beginning an x × y rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1 ≤ x ≤ n, 1 ≤ y ≤ m). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.
You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush.
InputThe first line of input contains two integers n and m, (1 ≤ n, m ≤ 1000), denoting the height and width of the painting.
The next n lines contain the painting. Each line has m characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting.
OutputPrint the minimum area of the brush in a line, if the painting is possibly altered, otherwise print - 1.
Sample test(s) input4 4XX.. XX.. XXXX XXXXoutput
4input
4 4.... .XXX .XXX ....output
2input
4 5 XXXX. XXXX. .XX.. .XX..output
-1
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=1100; const int INF=0x3f3f3f3f; char mp[maxn][maxn]; int n,m,sum[maxn][maxn]; int ans=INF; int area(int x1,int y1,int x2,int y2) { return sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]; } int dfs(int x,int y,int wx,int wy) { if(area(x,y+1,x+wx-1,y+wy)==wx*wy) return wx+dfs(x,y+1,wx,wy); if(area(x+1,y,x+wx,y+wy-1)==wx*wy) return wy+dfs(x+1,y,wx,wy); return wx*wy; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); bool flag=false; int px=-1,py=-1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { int t=0; if(mp[i][j]=='X') { if(flag==false) { flag=true; px=i; py=j; } t=1; } sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+t; } } int temp=py; for(;temp<=m;temp++) if(mp[px][temp]!='X') break; int leny=temp-py; int lenx; for(int i=px;i<=n;i++) { if(mp[i][py]!='X') break; lenx=i-px+1; if(dfs(px,py,lenx,leny)==sum [m]) { ans=min(ans,lenx*leny); } } temp=px; for(;temp<=n;temp++) if(mp[temp][py]!='X') break; lenx=temp-px; for(int i=py;i<=m;i++) { if(mp[px][i]!='X') break; leny=i-py+1; if(dfs(px,py,lenx,leny)==sum [m]) { ans=min(ans,lenx*leny); } } if(ans==INF) ans=-1; printf("%d\n",ans); return 0; }
- 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 475C Kamal-ol-molk's Painting 模拟
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- codeforce 9C Hexadecimal's Numbers
- 【CODEFORCES】 C. Kamal-ol-molk's Painting
- Codeforce 题目118D. Caesar's Legions(DP背包)
- YII错误base/CComponent.php(616) : eval()'d code on line 1
- make 'FORCE' does not exist - don't know how to make it 解决
- Method 'send' in the COM object of class 'Com.Message' returned error code 0x8004020F
- 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
- faultCode:Client.Error.MessageSend faultString:'Send failed' faultDetail:'Channel.Connect.Failed error NetConnection.Call.Failed
- .更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found
- RPC Fault faultString="Cannot invoke method 'helloWorld'." faultCode="Server.ResourceUnavailable" fa
- The Most Important Code Isn't Code
- CodeSourcery's GNU Toolchains的改名命令集:转arm-none-linux-gnueabi-到arm-linux-
- The virtual machine 'xiazhixing' has terminated unexpectedly during startup with exit code 1.解决办法
- C++模板继承在g++编译中应注意的问题(if you use `-fpermissive', G++ will accept your code, but allowing the use ...)
- 特殊Url处理。例如addActionpop.htm?url='http://localhost:8080/PowerPlayer.p3x?Tid=1826&Cid=152728&Fid=376424'&code=eeee@MG