Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstring> #include <cstdio> #include <algorithm> const int MAXN = 105; using namespace std; int H, W, ER, EC; int prefR[MAXN][MAXN], prefC[MAXN][MAXN], dp[MAXN][MAXN][MAXN][MAXN]; char str[2]; inline int sumR(int x, int l, int r) { if(l > r) return 0; return prefR[x][r] - prefR[x][l - 1]; } inline int sumC(int x, int l, int r) { if(l > r) return 0; return prefC[r][x] - prefC[l - 1][x]; } inline void update(int &x, int y) { x = (x > y) ? x : y; } int main() { scanf("%d%d", &H, &W); for(int r = 1; r <= H; r++) for(int c = 1; c <= W; c++) { scanf("%1s", str); if(str[0] == 'o') prefR[r][c] = prefC[r][c] = 1; else if(str[0] == 'E') ER = r, EC = c; } for(int r = 1; r <= H; r++) for(int c = 1; c <= W; c++) prefR[r][c] += prefR[r][c - 1]; for(int c = 1; c <= W; c++) for(int r = 1; r <= H; r++) prefC[r][c] += prefC[r - 1][c]; int Ans = 0; for(int l = 0; EC - l >= 1; l++) for(int r = 0; EC + r <= W; r++) for(int u = 0; ER - u >= 1; u++) for(int d = 0; ER + d <= H; d++) { int RL = d + 1, RR = H - u, CL = r + 1, CR = W - l; update(dp[l + 1][r][u][d], dp[l][r][u][d] + ( (EC - (l + 1) >= CL) ? sumC( EC - (l + 1), max(RL, ER - u), min(RR, ER + d) ) : 0 ) ); update(dp[l][r + 1][u][d], dp[l][r][u][d] + ( (EC + (r + 1) <= CR) ? sumC( EC + (r + 1), max(RL, ER - u), min(RR, ER + d) ) : 0 ) ); update(dp[l][r][u + 1][d], dp[l][r][u][d] + ( (ER - (u + 1) >= RL) ? sumR( ER - (u + 1), max(CL, EC - l), min(CR, EC + r) ) : 0 ) ); update(dp[l][r][u][d + 1], dp[l][r][u][d] + ( (ER + (d + 1) <= RR) ? sumR( ER + (d + 1), max(CL, EC - l), min(CR, EC + r) ) : 0 ) ); update(Ans, dp[l][r][u][d]); } printf("%d", Ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<cstdlib> #include<iostream> #include<cassert> #include<cstring> #include<algorithm> using namespace std; const int N = 100; short dp[N+3][N+3][N+3][N+3]; int s[N+3][N+3]; char a[N+3][N+3]; int n,m,sx,sy; int getsum(int lx,int rx,int ly,int ry) {return s[rx][ry]-s[lx-1][ry]-s[rx][ly-1]+s[lx-1][ly-1];} int updmax(short &x,short y) {x = x>y?x:y;} int main() { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%s",a[i]+1); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { s[i][j] = (a[i][j]=='o'?1:0)+s[i-1][j]+s[i][j-1]-s[i-1][j-1]; if(a[i][j]=='E') {sx = i,sy = j;} } } memset(dp,213,sizeof(dp)); dp[0][0][0][0] = 0; short ans = 0; for(int l=0; sy-l>0; l++) { for(int r=0; sy+r<=m; r++) { for(int u=0; sx-u>0; u++) { for(int d=0; sx+d<=n; d++) { updmax(ans,dp[l][r][u][d]); int ly = max(1+r,sy-l),ry = min(m-l,sy+r),lx = max(1+d,sx-u),rx = min(n-u,sx+d); // printf("l%d r%d u%d d%d x[%d,%d] y[%d,%d]\n",l,r,u,d,lx,rx,ly,ry); if(sx+d<n-u) { updmax(dp[l][r][u][d+1],dp[l][r][u][d]+getsum(sx+d+1,sx+d+1,ly,ry)); } if(sx-u>1+d) { updmax(dp[l][r][u+1][d],dp[l][r][u][d]+getsum(sx-u-1,sx-u-1,ly,ry)); } if(sy+r<m-l) { updmax(dp[l][r+1][u][d],dp[l][r][u][d]+getsum(lx,rx,sy+r+1,sy+r+1)); } if(sy-l>1+r) { updmax(dp[l+1][r][u][d],dp[l][r][u][d]+getsum(lx,rx,sy-l-1,sy-l-1)); } } } } } printf("%d\n",(int)ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
// Salvage Robots // * frank_c1 // * 2017 / 12 / 17 #include <bits/stdc++.h> using namespace std; const int maxn = 105; char s[maxn][maxn]; int r[maxn][maxn], c[maxn][maxn]; int f[maxn * maxn >> 2][maxn * maxn >> 2]; int h, w, X, Y; inline int encode(int l, int r, int M) { return r * M + l; } inline void chkmax(int& x, int v) { x = max(x, v); } int dfs(int lX, int rX, int lY, int rY) { int cX = encode(lX, rX, X), cY = encode(lY, rY, Y); if (f[cX][cY] >= 0) return f[cX][cY]; f[cX][cY] = 0; if (lX < X - 1) { chkmax(f[cX][cY], dfs(lX + 1, rX, lY, rY) + (rX + 1 <= X - lX - 1 ? max(0, r[X - lX - 1][min(Y + rY, w - lY)] - r[X - lX - 1][max(Y - lY, rY + 1) - 1]) : 0)); } if (rX < h - X) { chkmax(f[cX][cY], dfs(lX, rX + 1, lY, rY) + (X + rX + 1 <= h - lX ? max(0, r[X + rX + 1][min(Y + rY, w - lY)] - r[X + rX + 1][max(Y - lY, rY + 1) - 1]) : 0)); } if (lY < Y - 1) { chkmax(f[cX][cY], dfs(lX, rX, lY + 1, rY) + (rY + 1 <= Y - lY - 1 ? max(0, c[min(X + rX, h - lX)][Y - lY - 1] - c[max(X - lX, rX + 1) - 1][Y - lY - 1]) : 0)); } if (rY < w - Y) { chkmax(f[cX][cY], dfs(lX, rX, lY, rY + 1) + (Y + rY + 1 <= w - lY ? max(0, c[min(X + rX, h - lX)][Y + rY + 1] - c[max(X - lX, rX + 1) - 1][Y + rY + 1]) : 0)); } return f[cX][cY]; } int main() { scanf("%d%d", &h, &w); for (int i = 1; i <= h; ++i) scanf("%s", s[i] + 1); for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) if (s[i][j] == 'E') X = i, Y = j; for (int i = 1; i <= h; ++i) for (int j = 1; j <= w; ++j) { r[i][j] = r[i][j - 1] + (s[i][j] == 'o'); c[i][j] = c[i - 1][j] + (s[i][j] == 'o'); } memset(f, -1, sizeof(f)); return printf("%d\n", dfs(0, 0, 0, 0)), 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using std::min; using std::max; char s[105]; int a[205][205],ans,h,w,sx,sy; short dp[105][105][105][105]; short sh[105][105],sl[105][105]; #define ch(x,l,r) sh[x][r]-sh[x][l-1] #define cl(x,l,r) sl[x][r]-sl[x][l-1] #define m(x,y) x=x>y?x:y int main(){ scanf("%d%d",&h,&w); for (int i=1;i<=h;i++) { scanf("%s",s+1); for (int j=1;j<=w;j++){ if (s[j]=='o') a[i][j]=1; if (s[j]=='E') sx=i,sy=j; } } for (int i=1;i<=h;i++) for (int j=1;j<=w;j++) sh[i][j]=a[i][j]+sh[i][j-1],sl[j][i]=a[i][j]+sl[j][i-1]; for (int i=0;i<=h+1;i++) for (int j=0;j<=w+1;j++) for (int i2=0;i2<=h+1;i2++) for (int j2=0;j2<=w+1;j2++) dp[i][j][i2][j2]=-1; dp[sx][sy][sx][sy]=0; for (int i=sx;i>=1;i--) for (int j=sy;j>=1;j--) for (int i2=sx;i2<=h;i2++) for (int j2=sy;j2<=w;j2++){ if (i==sx && i2==sx && j==sy && j2==sy) continue; if (dp[i+1][j][i2][j2]>=0 && i>i2-sx && i!=sx) m(dp[i][j][i2][j2],dp[i+1][j][i2][j2]+ch(i,max(j,j2-sy+1),min(j2,w-(sy-j)))); if (dp[i][j][i2-1][j2]>=0 && i2<=h-(sx-i) && i2!=sx) m(dp[i][j][i2][j2],dp[i][j][i2-1][j2]+ch(i2,max(j,j2-sy+1),min(j2,w-(sy-j)))); if (dp[i][j+1][i2][j2]>=0 && j>j2-sy && j!=sy) m(dp[i][j][i2][j2],dp[i][j+1][i2][j2]+cl(j,max(i,i2-sx+1),min(i2,h-(sx-i)))); if (dp[i][j][i2][j2-1]>=0 && j2<=w-(sy-j) && j2!=sy) m(dp[i][j][i2][j2],dp[i][j][i2][j2-1]+cl(j2,max(i,i2-sx+1),min(i2,h-(sx-i)))); m(ans,dp[i][j][i2][j2]); } printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> #include <set> #include <queue> using namespace std; int sum1[105][105],sum2[105][105]; int f[2][105][105][105]; char s[105]; int main(){ int n,i,j,k,o,p,l,m; int tx,ty; scanf("%d %d",&n,&m); for(i=1;i<=n;i++){ scanf("%s",s+1); for(j=1;j<=m;j++) if(s[j]=='E')tx=i,ty=j; else if(s[j]=='o')sum1[i][j]=sum1[i][j-1]+1,sum2[i][j]=sum2[i-1][j]+1; else sum1[i][j]=sum1[i][j-1],sum2[i][j]=sum2[i-1][j]; } int limu=tx-1,limd=n-tx,liml=ty-1,limr=m-ty; for(int u=0;u<=limu;u++) for(int d=0;d<=limd;d++) for(int l=0;l<=liml;l++) for(int r=0;r<=limr;r++){ int L=max(ty-l,r+1),R=min(ty+r,m-l); if(L<=R){ f[u+1&1][d][l][r]=max(f[u+1&1][d][l][r],f[u&1][d][l][r]+(tx-u-1>=d+1?sum1[tx-u-1][R]-sum1[tx-u-1][L-1]:0)); f[u&1][d+1][l][r]=max(f[u&1][d+1][l][r],f[u&1][d][l][r]+(tx+d+1<=n-u?sum1[tx+d+1][R]-sum1[tx+d+1][L-1]:0)); } L=max(tx-u,d+1),R=min(tx+d,n-u); if(L<=R){ f[u&1][d][l+1][r]=max(f[u&1][d][l+1][r],f[u&1][d][l][r]+(ty-l-1>=r+1?sum2[R][ty-l-1]-sum2[L-1][ty-l-1]:0)); f[u&1][d][l][r+1]=max(f[u&1][d][l][r+1],f[u&1][d][l][r]+(ty+r+1<=m-l?sum2[R][ty+r+1]-sum2[L-1][ty+r+1]:0)); } } printf("%d\n",f[limu&1][limd][liml][limr]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <algorithm> #include <cstdio> char str[105]; int row_pre[105][105], col_pre[105][105], dp[105][105][105][105]; int main() { // freopen("AGC004-E.in", "r", stdin); int n, m, x, y; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", str); for (int j = 0; j < m; j++) { row_pre[i][j] = (j ? row_pre[i][j - 1] : 0) + (str[j] == 'o'); col_pre[i][j] = (i ? col_pre[i - 1][j] : 0) + (str[j] == 'o'); if (str[j] == 'E') { x = i; y = j; } } } auto calc_row = [&] (int idx, int l, int r) { int ll = std::max(l, r - y), rr = std::min(r, m - 1 - y + l); if (ll > rr) return 0; return row_pre[idx][rr] - (ll ? row_pre[idx][ll - 1] : 0); }; auto calc_col = [&] (int idx, int l, int r) { int ll = std::max(l, r - x), rr = std::min(r, n - 1 - x + l); if (ll > rr) return 0; return col_pre[rr][idx] - (ll ? col_pre[ll - 1][idx] : 0); }; auto upd = [&] (int &a, const int &b) { if (a < b) a = b; }; for (int i = 0; i <= x; i++) { for (int j = n - 1; j >= x; j--) { for (int k = 0; k <= y; k++) { for (int t = m - 1; t >= y; t--) { if (i > j - x) upd(dp[i][j][k][t], dp[i - 1][j][k][t] + calc_row(i - 1, k, t)); if (j < n - 1 - x + i) upd(dp[i][j][k][t], dp[i][j + 1][k][t] + calc_row(j + 1, k, t)); if (k > t - y) upd(dp[i][j][k][t], dp[i][j][k - 1][t] + calc_col(k - 1, i, j)); if (t < m - 1 - y + k) upd(dp[i][j][k][t], dp[i][j][k][t + 1] + calc_col(t + 1, i, j)); } } } } printf("%d\n", dp[x][x][y][y]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <algorithm> const int maxn = 110; int a[maxn][maxn], x, y, n, m, dp[maxn][maxn][maxn], pd[maxn][maxn][maxn], ans, x1, Y1, x2, y2; inline void upd(int &x, int y) { x = x < y ? y : x; } inline int qry(int x1, int Y1, int x2, int y2) { x1 = std::max(x1, ::x1); Y1 = std::max(Y1, ::Y1); x2 = std::min(x2, ::x2); y2 = std::min(y2, ::y2); if(x1 > x2 || Y1 > y2) { return 0; } return a[x2][y2] - a[x1 - 1][y2] - a[x2][Y1 - 1] + a[x1 - 1][Y1 - 1]; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { char c = getchar(); while(c != '.' && c != 'o' && c != 'E') { c = getchar(); } if(c == 'o') { a[i][j] = 1; } if(c == 'E') { x = i; y = j; } a[i][j] -= a[i - 1][j - 1] - a[i - 1][j] - a[i][j - 1]; } } memset(dp, -1, sizeof dp); dp[0][0][0] = 0; for (int l = 0; l <= m; ++l) { memset(pd, -1, sizeof pd); for (int u = 0; u <= n; ++u) { for (int r = 0; r + l <= m; ++r) { for (int t, d = 0; d + u <= n; ++d) { if(~(t = dp[u][r][d])) { ans = std::max(ans, t); x1 = u + 1, Y1 = l + 1, x2 = n - d, y2 = m - r; upd(pd[u][r][d], t + qry(x - d, y + l + 1, x + u, y + l + 1)); upd(dp[u + 1][r][d], t + qry(x + u + 1, y - r, x + u + 1, y + l)); upd(dp[u][r + 1][d], t + qry(x - d, y - r - 1, x + u, y - r - 1)); upd(dp[u][r][d + 1], t + qry(x - d - 1, y - r, x - d - 1, y + l)); } } } } memcpy(dp, pd, sizeof dp); } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; const int N=110, M=5e3+300; int h,w,F[M][M],idh[N][N],idw[N][N]; char mp[N][N]; int stx,sty,s[N][N]; inline int& f(int l,int r,int u,int d) {return F[idh[l][r]][idw[u][d]];} inline int getarea(int xc1,int xc2,int yc1,int yc2) { return s[xc2][yc2]-s[xc1-1][yc2]-s[xc2][yc1-1]+s[xc1-1][yc1-1]; } inline int calcl(int p,int up,int dn,int l,int r,int u,int d) { if(p<=r || p+l>w) return 0; up=max(up,d+1); dn=min(dn,h-u); if(up>dn) return 0; return getarea(up,dn,p,p); } inline int calcr(int p,int li,int ri,int l,int r,int u,int d) { if(p<=d || p+u>h) return 0; li=max(li,r+1); ri=min(ri,w-l); if(li>ri) return 0; return getarea(p,p,li,ri); } int main() { cin>>h>>w; for(int i=1;i<=h;i++) cin>>(mp[i]+1); for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) { if(mp[i][j]=='o') s[i][j]=1; if(mp[i][j]=='E') stx=i, sty=j; s[i][j]=s[i][j-1]+s[i-1][j]-s[i-1][j-1]+s[i][j]; } for(int l=0,cnt=0;l<=sty;++l) for(int r=0;sty+r<=w;++r) idh[l][r]=cnt++; for(int u=0,cnt=0;u<=stx;++u) for(int d=0;d+stx<=h;++d) idw[u][d]=cnt++; int ans=0; for(int l=0;l<=sty;++l) for(int r=0;sty+r<=w;++r) for(int u=0;u<=stx;++u) for(int d=0;d+stx<=h;++d) { int &s=f(l,r,u,d); if(l) s=max(s,f(l-1,r,u,d)+calcl(sty-l,stx-u,stx+d,l-1,r,u,d)); if(r) s=max(s,f(l,r-1,u,d)+calcl(sty+r,stx-u,stx+d,l,r-1,u,d)); if(u) s=max(s,f(l,r,u-1,d)+calcr(stx-u,sty-l,sty+r,l,r,u-1,d)); if(d) s=max(s,f(l,r,u,d-1)+calcr(stx+d,sty-l,sty+r,l,r,u,d-1)); ans=max(ans,s); } cout<<ans<<'\n'; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> int H,W,Sr[111][111],Sc[111][111],f[111][111][111],Ex,Ey; char a[111][111]; int rows(int i,int l,int r){return l<=r?Sr[i][r]-Sr[i][l-1]:0;} int cols(int i,int l,int r){return l<=r?Sc[r][i]-Sc[l-1][i]:0;} int min(int a,int b){return a<b?a:b;} int max(int a,int b){return a>b?a:b;} void cmax(int&a,int b){b>a?a=b:1;} int main(){ scanf("%d%d",&H,&W); for(int i=1;i<=H;i++){ scanf("%s",a[i]+1); for(int j=1;j<=W;j++){ Sr[i][j]=Sr[i][j-1]+(a[i][j]=='o'); Sc[i][j]=Sc[i-1][j]+(a[i][j]=='o'); if(a[i][j]=='E')Ex=i,Ey=j; } } for(int u=1,rd=H-Ex+1;u<=Ex;u++,rd++) for(int d=H,ru=H-Ex+1;d>=Ex;d--,ru--) for(int l=1,rr=W-Ey+1;l<=Ey;l++,rr++) for(int r=W,rl=W-Ey+1;r>=Ey;r--,rl--){ if(u-1>=ru)f[d][l][r]+=rows(u-1,max(l,rl),min(r,rr)); if(d+1<=rd)cmax(f[d][l][r],f[d+1][l][r]+rows(d+1,max(l,rl),min(r,rr))); if(l-1>=rl)cmax(f[d][l][r],f[d][l-1][r]+cols(l-1,max(u,ru),min(d,rd))); if(r+1<=rr)cmax(f[d][l][r],f[d][l][r+1]+cols(r+1,max(u,ru),min(d,rd))); } printf("%d\n",f[Ex][Ey][Ey]); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int n,m,Ex,Ey,Ans,f[2][110][110][110]; int g[110][110],row[110][110],line[110][110]; inline int Row(int id,int L,int R) {return row[id][R]-row[id][L-1];} inline int Line(int U,int D,int id) {return line[D][id]-line[U-1][id];} int main() { //#ifdef h10 // freopen("robots.in","r",stdin); // freopen("robots.out","w",stdout); //#endif int i,j,U,D,L,R; char c; scanf("%d%d",&n,&m); for (i=1;i<=n;i++) for (j=1;j<=m;j++) { c=getchar(); while (c!='.'&&c!='o'&&c!='E') c=getchar(); if (c=='o') g[i][j]=1; if (c=='E') Ex=i,Ey=j; row[i][j]=row[i][j-1]+g[i][j]; line[i][j]=line[i-1][j]+g[i][j]; } for (U=0;U<Ex;U++) for (D=0;D<=n-Ex;D++) for (L=0;L<Ey;L++) for (R=0;R<=m-Ey;R++) { int X=U&1,Y=X^1,&val=f[X][D][L][R]; int u=max(D+1,Ex-U),d=min(Ex+D,n-U); int l=max(R+1,Ey-L),r=min(Ey+R,m-L); if (U&&Ex-U-D>0) val=max(val,f[Y][D][L][R]+Row(Ex-U,l,r)); if (D&&Ex+D+U<=n) val=max(val,f[X][D-1][L][R]+Row(Ex+D,l,r)); if (L&&Ey-L-R>0) val=max(val,f[X][D][L-1][R]+Line(u,d,Ey-L)); if (R&&Ey+R+L<=m) val=max(val,f[X][D][L][R-1]+Line(u,d,Ey+R)); Ans=max(Ans,val); } printf("%d\n",Ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i <= b; i++) #define per(i, a, b) for(int i = a; i >= b; i--) typedef long long LL; const int maxn = 105; const LL mod = 1e9 + 7; int n, m, x, y; int f[maxn][maxn][maxn][maxn], r[maxn][maxn], c[maxn][maxn]; char ch; int main() { cin>>n>>m; rep(i, 1, n) rep(j, 1, m) { cin>>ch; if(ch == 'E') x = i, y = j; r[i][j] = r[i][j - 1]; c[i][j] = c[i - 1][j]; if(ch == 'o') r[i][j]++, c[i][j]++; } int A, B, C, D; A = x - 1, B = n - x; C = y - 1, D = m - y; rep(i, 0, A) rep(j, 0, B) rep(k, 0, C) rep(l, 0, D) { int val = f[i][j][k][l]; int L = max(y - k, l + 1), R = min(y + l, m - k); f[i + 1][j][k][l] = max(f[i + 1][j][k][l], val + (r[x - i - 1][R] - r[x - i - 1][L - 1])*(x - i - 1 > j)); f[i][j + 1][k][l] = max(f[i][j + 1][k][l], val + (r[x + j + 1][R] - r[x + j + 1][L - 1])*(x + j + 1 + i <= n)); L = max(x - i, j + 1), R = min(x + j, n - i); f[i][j][k + 1][l] = max(f[i][j][k + 1][l], val + (c[R][y - k - 1] - c[L - 1][y - k - 1])*(y - k - 1 > l)); f[i][j][k][l + 1] = max(f[i][j][k][l + 1], val + (c[R][y + l + 1] - c[L - 1][y + l + 1])*(y + l + 1 + k <= m)); } cout<<f[A][B][C][D]<<endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <ctime> #include <queue> #include <set> #include <map> #include <string> #include <bitset> #include <vector> #include <complex> #include <algorithm> using namespace std; typedef long long ll; typedef double db; typedef pair<int,int> pii; typedef vector<int> vi; #define de(x) cout << #x << "=" << x << endl #define rep(i,a,b) for(int i=a;i<(b);++i) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define fi first #define se second const int N = 101; int f[N][N][N][N] , col[N][N] , row[N][N] , H , W , ex , ey; char s[N][N]; inline void Update(int &a,int d){ if(d > a) a = d; } inline int Row(int x,int a,int b){ return row[x][b] - row[x][a-1]; } inline int Col(int y,int a,int b){ return col[b][y] - col[a-1][y]; } int main(){ scanf("%d%d",&H,&W); rep(i,1,H+1) scanf("%s",s[i] + 1); rep(i,1,H+1) rep(j,1,W+1){ col[i][j] = col[i-1][j] + (s[i][j] == 'o'); row[i][j] = row[i][j-1] + (s[i][j] == 'o'); if(s[i][j] == 'E') ex = i , ey = j; } int _L = ey , _R = W-ey+1 , _U = ex , _D = H-ex+1; rep(l,0,_L) rep(r,0,_R) rep(u,0,_U) rep(d,0,_D){ int t = f[l][r][u][d]; //cout << l << " " << r << " " << u << " " << d << endl; int U = max(ex-u,1+d) , D = min(ex+d,H-u); Update(f[l+1][r][u][d] , t + (U<=D && 1+r<=ey-l-1 ? Col(ey-l-1,U,D) : 0)); Update(f[l][r+1][u][d] , t + (U<=D && ey+r+1<=W-l ? Col(ey+r+1,U,D) : 0)); int L = max(ey-l,1+r) , R = min(ey+r,W-l); Update(f[l][r][u+1][d] , t + (L<=R && 1+d<=ex-u-1 ? Row(ex-u-1,L,R) : 0)); Update(f[l][r][u][d+1] , t + (L<=R && ex+d+1<=H-u ? Row(ex+d+1,L,R) : 0)); } printf("%d\n",f[_L-1][_R-1][_U-1][_D-1]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define N 110 using namespace std; int sum[N][N],f[N][N][N]; int n,m,sx,sy; char s[N]; int ans; int sm(int x1,int y1,int x2,int y2) { return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=m;j++) { if(s[j]=='o')sum[i][j]++; else if(s[j]=='E')sx=i,sy=j; } } for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]; memset(f,0xcf,sizeof(f)); f[0][0][0]=0; for(int u=0;u<=n;u++) { if(sx-u<1)break; for(int d=0;d<=n;d++) { if(sx+d>n)break; int up=max(sx-u,d+1); int down=min(sx+d,n-u); if(up>down)continue; for(int l=0;l<=m;l++) { if(sy-l<1)break; for(int r=0;r<=m;r++) { if(sy+r>m)continue; int left=max(sy-l,r+1); int right=min(sy+r,m-l); if(left>right)continue; ans=max(ans,f[d][l][r]); if(u+d<n-sx)f[d+1][l][r]=max(f[d+1][l][r],f[d][l][r]+sm(sx+d+1,left,sx+d+1,right)); if(l+r<m-sy)f[d][l][r+1]=max(f[d][l][r+1],f[d][l][r]+sm(up,sy+r+1,down,sy+r+1)); if(l+r<sy-1)f[d][l+1][r]=max(f[d][l+1][r],f[d][l][r]+sm(up,sy-l-1,down,sy-l-1)); if(u+d<sx-1)f[d][l][r]=max(f[d][l][r],f[d][l][r]+sm(sx-u-1,left,sx-u-1,right)); } } } } cout<<ans<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstdio> using namespace std; char c; int n,m; int f[110][110][110][110]; int linex[110][110],liney[110][110]; int X,Y; struct LIMIT{ int u,d,l,r; }L; int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ scanf("%c",&c); while (c!='.'&&c!='o'&&c!='E') scanf("%c",&c); if (c=='E') X=i,Y=j; linex[i][j]=linex[i][j-1]; liney[i][j]=liney[i-1][j]; if (c=='o'){ linex[i][j]++; liney[i][j]++; } } L.u=X-1;L.d=n-X;L.l=Y-1;L.r=m-Y; for(int u=0;u<=L.u;u++) for(int d=0;d<=L.d;d++) for(int l=0;l<=L.l;l++) for(int r=0;r<=L.r;r++){ int L=max(Y-l,r+1),R=min(Y+r,m-l); if(L<=R){ f[u+1][d][l][r]=max(f[u+1][d][l][r],f[u][d][l][r]+(X-u-1>=d+1?linex[X-u-1][R]-linex[X-u-1][L-1]:0)); f[u][d+1][l][r]=max(f[u][d+1][l][r],f[u][d][l][r]+(X+d+1<=n-u?linex[X+d+1][R]-linex[X+d+1][L-1]:0)); } L=max(X-u,d+1); R=min(X+d,n-u); if(L<=R){ f[u][d][l+1][r]=max(f[u][d][l+1][r],f[u][d][l][r]+(Y-l-1>=r+1?liney[R][Y-l-1]-liney[L-1][Y-l-1]:0)); f[u][d][l][r+1]=max(f[u][d][l][r+1],f[u][d][l][r]+(Y+r+1<=m-l?liney[R][Y+r+1]-liney[L-1][Y+r+1]:0)); } } printf("%d\n",f[L.u][L.d][L.l][L.r]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int res,ad,n,m,ll,rr,uu,dd,L,R,U,D,sx,sy,dp[105][105][105],row[105][105],col[105][105]; char s[105][105]; int main() { memset(dp,-1,sizeof dp); dp[0][0][0]=0; scanf("%d%d",&n,&m); for (int i=1;i<=n;++i) scanf("%s",s[i]+1); for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) { row[i][j]+=row[i][j-1]+(s[i][j]=='o'); col[i][j]+=col[i-1][j]+(s[i][j]=='o'); if (s[i][j]=='E') sx=i,sy=j; } L=sy-1,R=m-sy,U=sx-1,D=n-sx; for (int l=0;l<=L;++l) for (int r=0;r<=R;++r) for (int u=0;u<=U;++u) for (int d=0;d<=D;++d) { if (dp[r][u][d]==-1) continue; res=max(res,dp[r][u][d]); ll=max(sy-l,r+1); rr=min(sy+r,m-l); dd=max(sx-u,d+1); uu=min(sx+d,n-u); if (u+d<D) { ad=row[sx+d+1][rr]-row[sx+d+1][ll-1]; dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+ad); } if (u+d<U) { ad=row[sx-u-1][rr]-row[sx-u-1][ll-1]; dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+ad); } if (l+r<R) { ad=col[uu][sy+r+1]-col[dd-1][sy+r+1]; dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+ad); } if (l+r<L) { ad=col[uu][sy-l-1]-col[dd-1][sy-l-1]; dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+ad); } } printf("%d\n",res); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int MaxS = 105, MaxN = 5050; int f[MaxN][MaxN], px[MaxS][MaxS], py[MaxS][MaxS], h, w, Ex, Ey, qx[MaxS][MaxS], qy[MaxS][MaxS]; char ma[MaxS][MaxS]; /* l u d r */ int calcx (int x, int l, int r, int cl, int cr) { if (x < cl || x > cr) return 0; if (l > r) puts("A"); return qx[x][r] - qx[x][l - 1]; } int calcy (int x, int l, int r, int cl, int cr) { if (x < cl || x > cr) return 0; if (l > r) puts("A"); return qy[x][r] - qy[x][l - 1]; } int dp (int l, int r, int u, int d) { // if (px[l][r] == -1 || py[u][d] == -1) return 0; if (l == r && u == d) return 0; if (~f[px[l][r]][py[u][d]]) return f[px[l][r]][py[u][d]]; int &re = f[px[l][r]][py[u][d]]; ++re; int qlx = max((d - Ey) + 1, u), qrx = min(w - (Ey - u), d); int qly = max((r - Ex) + 1, l), qry = min(h - (Ex - l), r); if (~px[l + 1][r]) re = max(re, dp(l + 1, r, u, d) + calcx(l, qlx, qrx, qly, qry)); if (~px[l][r - 1]) re = max(re, dp(l, r - 1, u, d) + calcx(r, qlx, qrx, qly, qry)); if (~py[u + 1][d]) re = max(re, dp(l, r, u + 1, d) + calcy(u, qly, qry, qlx, qrx)); if (~py[u][d - 1]) re = max(re, dp(l, r, u, d - 1) + calcy(d, qly, qry, qlx, qrx)); return re; } int main () { memset(f, -1, sizeof f); memset(py, -1, sizeof py); memset(px, -1, sizeof px); scanf("%d%d", &h, &w); for (int i = 1; i <= h; ++i) { scanf("%s", ma[i] + 1); for (int j = 1; j <= w; ++j) { if (ma[i][j] == 'E') Ex = i, Ey = j; qx[i][j] = qx[i][j - 1] + (ma[i][j] == 'o'); qy[j][i] = qy[j][i - 1] + (ma[i][j] == 'o'); } } for (int i = 1, c = 0; i <= Ex; ++i) for (int j = Ex; j <= h; ++j) px[i][j] = c++; for (int i = 1, c = 0; i <= Ey; ++i) for (int j = Ey; j <= w; ++j) py[i][j] = c++; printf("%d\n", dp(1, h, 1, w)); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <stack> #include <vector> #define MAXN 110 #define MAXS 5100 using namespace std; int n,m; int a[MAXN][MAXN],s[MAXN][MAXN],ex,ey; int px[MAXN][MAXN],py[MAXN][MAXN],numpx,numpy; int f[MAXS][MAXS]; int getS(int x1,int x2,int y1,int y2){ return s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1]; } int trans(int x1,int x2,int y1,int y2,int x3,int x4,int y3,int y4){ int tx1=x2-n+ex,tx2=x1+ex-1; int ty1=y2-m+ey,ty2=y1+ey-1; if(tx1>x3) x3=tx1; if(tx2<x4) x4=tx2; if(ty1>y3) y3=ty1; if(ty2<y4) y4=ty2; if(x3<=x4 && y3<=y4) return getS(x3,x4,y3,y4); else return 0; } int gao(int x1,int x2,int y1,int y2){ if(x1==x2 && y1==y2) return a[x1][y1]; int res=0; if(x1<x2){ res=max(res,f[px[x1][x2-1]][py[y1][y2]]+trans(x1,x2-1,y1,y2,x2,x2,y1,y2)); res=max(res,f[px[x1+1][x2]][py[y1][y2]]+trans(x1+1,x2,y1,y2,x1,x1,y1,y2)); } if(y1<y2){ res=max(res,f[px[x1][x2]][py[y1][y2-1]]+trans(x1,x2,y1,y2-1,x1,x2,y2,y2)); res=max(res,f[px[x1][x2]][py[y1+1][y2]]+trans(x1,x2,y1+1,y2,x1,x2,y1,y1)); } return res; } int main(){ #ifdef DEBUG freopen("E.in","r",stdin); #endif scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ static char str[MAXN]; scanf("%s",str+1); for(int j=1;j<=m;j++) if(str[j]=='o'){ a[i][j]=1; }else if(str[j]=='E'){ ex=i; ey=j; } } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1]; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) px[i][j]=++numpx; for(int i=1;i<=m;i++) for(int j=i;j<=m;j++) py[i][j]=++numpy; for(int w=1;w<=n;w++) for(int h=1;h<=m;h++) for(int i=1;i+w-1<=n;i++) for(int j=1;j+h-1<=m;j++) f[px[i][i+w-1]][py[j][j+h-1]]=gao(i,i+w-1,j,j+h-1); printf("%d\n",f[px[1][n]][py[1][m]]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; int n, m, ex, ey; char a[105][105]; short dp[101][101][101][101], sr[105][105], sc[105][105]; inline void getmax(short &x, short y) { if (x < y) x = y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf(" %s", a[i] + 1); for (int j = 1; j <= m; j++) { if (a[i][j] == 'E') { ex = i; ey = j; a[i][j] = '.'; } sr[i][j] = sc[i][j] = (a[i][j] == 'o'); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { sr[i][j] += sr[i][j - 1]; sc[i][j] += sc[i - 1][j]; } } int ans = 0; for (int l = 0; l <= ey - 1; l++) { for (int r = 0; r <= m - ey; r++) { for (int u = 0; u <= ex - 1; u++) { for (int d = 0; d <= n - ex; d++) { ans = max(ans, (int)dp[l][r][u][d]); if (l + r < ey - 1) getmax(dp[l + 1][r][u][d], dp[l][r][u][d] + sc[min(n - u, ex + d)][ey - l - 1] - sc[max(d, ex - u - 1)][ey - l - 1]); if (l + r < m - ey) getmax(dp[l][r + 1][u][d], dp[l][r][u][d] + sc[min(n - u, ex + d)][ey + r + 1] - sc[max(d, ex - u - 1)][ey + r + 1]); if (u + d < ex - 1) getmax(dp[l][r][u + 1][d], dp[l][r][u][d] + sr[ex - u - 1][min(m - l, ey + r)] - sr[ex - u - 1][max(r, ey - l - 1)]); if (u + d < n - ex) getmax(dp[l][r][u][d + 1], dp[l][r][u][d] + sr[ex + d + 1][min(m - l, ey + r)] - sr[ex + d + 1][max(r, ey - l - 1)]); } } } } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define maxn 105 using namespace std; int n, m; char s[maxn][maxn]; short f[maxn][maxn][maxn][maxn]; int x, y; int sum1[maxn][maxn], sum2[maxn][maxn]; inline void Max(short &x, short y){ x = max(x, y); } int main(){ scanf("%d%d", &n, &m); for(int i = 1;i <= n;i++) scanf("%s", s[i] + 1); for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ if(s[i][j] == 'E') x = i, y = j; sum1[i][j] = sum1[i][j - 1] + (s[i][j] == 'o'); sum2[i][j] = sum2[i - 1][j] + (s[i][j] == 'o'); } } int U = x - 1, D = n - x, L = y - 1, R = m - y; for(int i = 0;i <= U;i++){ for(int j = 0;j <= D;j++){ for(int k = 0;k <= L;k++){ for(int o = 0;o <= R;o++){ int l = max(o + 1, y - k), r = min(m - k, y + o); if(i < U) Max(f[i + 1][j][k][o], f[i][j][k][o] + (l <= r && x - i - 1 > j ? sum1[x - i - 1][r] - sum1[x - i - 1][l - 1] : 0)); if(j < D) Max(f[i][j + 1][k][o], f[i][j][k][o] + (l <= r && x + j + 1 < n + 1 - i ? sum1[x + j + 1][r] - sum1[x + j + 1][l - 1] : 0)); int u = max(j + 1, x - i), d = min(n - i, x + j); if(k < L) Max(f[i][j][k + 1][o], f[i][j][k][o] + (u <= d && y - k - 1 > o ? sum2[d][y - k - 1] - sum2[u - 1][y - k - 1] : 0)); if(o < R) Max(f[i][j][k][o + 1], f[i][j][k][o] + (u <= d && y + o + 1 < m + 1 - k ? sum2[d][y + o + 1] - sum2[u - 1][y + o + 1] : 0));//, printf("%d %d %d %d %d %d %d %d %d %d--\n", i, j, k, o, l, r, u, d, y + o + 1, m + 1 - k); //printf("%d %d %d %d %d %d %d %d--\n", i, j, k, o, u, d, y - k - 1, f[i][j][k][o]); } } } } printf("%d", f[U][D][L][R]); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cmath> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <bitset> #include <set> const int maxlongint=2147483647; const int mo=1e9+7; const int N=105; using namespace std; short f[N][N][N][N],n,m,a[N][N],ans,sx,sy,v[N][N],v1[N][N]; int main() { //freopen("agc004e.in","r",stdin); //freopen("agc004e.out","w",stdout); cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char c=getchar(); while(c!='.' && c!='o' && c!='E') c=getchar(); v[i][j]=v1[i][j]=a[i][j]=(c=='o'); v[i][j]+=v[i][j-1]; v1[i][j]+=v1[i-1][j]; if(c=='E') sx=i,sy=j; } for(short i=sx;i>=1;i--) for(short j=sy;j>=1;j--) for(short i1=sx;i1<=n;i1++) for(short j1=sy;j1<=m;j1++) { if(1<i && i1+1<sx+i) f[i-1][j][i1][j1]=max((int)f[i-1][j][i1][j1],(int)f[i][j][i1][j1]+v[i-1][min((int)j1,m-sy+j)]-v[i-1][max((int)j-1,j1-sy)]); if(i1<n && sx+i1<n+i) f[i][j][i1+1][j1]=max((int)f[i][j][i1+1][j1],(int)f[i][j][i1][j1]+v[i1+1][min((int)j1,m-sy+j)]-v[i1+1][max((int)j-1,j1-sy)]); if(1<j && j1+1<sy+j) f[i][j-1][i1][j1]=max((int)f[i][j-1][i1][j1],(int)f[i][j][i1][j1]+v1[min((int)i1,n-sx+i)][j-1]-v1[max((int)i-1,i1-sx)][j-1]); if(j1<m && sy+j1<m+j) f[i][j][i1][j1+1]=max((int)f[i][j][i1][j1+1],(int)f[i][j][i1][j1]+v1[min((int)i1,n-sx+i)][j1+1]-v1[max((int)i-1,i1-sx)][j1+1]); } for(short i=sx;i>=1;i--) for(short j=sy;j>=1;j--) for(short i1=sx;i1<=n;i1++) for(short j1=sy;j1<=m;j1++) ans=max(f[i][j][i1][j1],ans); cout<<ans<<endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define ll long long #define ull unsigned ll #define uint unsigned #define db long double #define pii pair<int,int> #define pll pair<ll,ll> #define IT iterator #define PB push_back #define MK make_pair #define LB lower_bound #define UB upper_bound #define EB emplace_back #define fi first #define se second #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define UPD(x,y) (((x)+=(y))>=mo?x-=mo:233) #define CLR(a,v) memset(a,v,sizeof(a)); #define CPY(a,b) memcpy(a,b,sizeof(a)); #define LS3 k*2,l,mid #define RS3 k*2+1,mid+1,r #define LS5 k*2,l,mid,x,y #define RS5 k*2+1,mid+1,r,x,y #define GET pushdown(k);int mid=(l+r)/2 #define INF ((1ll<<60)-233) #define sqr(x) ((x)*(x)) #define debug puts("wzpkking") using namespace std; const int N=105; char mp[N][N]; int n,m,ex,ey,ans; int sr[N][N],sc[N][N]; int dp[N][N][N]; void mx(int &x,int y){ x<y?x=y:0; } int main(){ scanf("%d%d",&n,&m); CLR(dp,-1); For(i,1,n) scanf("%s",mp[i]+1); For(i,1,n) For(j,1,m) if (mp[i][j]=='o') sr[i][j]=sc[i][j]=1; else if (mp[i][j]=='E') ex=i,ey=j; For(i,1,n) For(j,1,m) sr[i][j]+=sr[i][j-1], sc[i][j]+=sc[i-1][j]; int L=ey-1,R=m-ey,U=ex-1,D=n-ex; dp[0][0][0]=0; For(l,0,L) For(r,0,R) For(u,0,U) For(d,0,D){ if (dp[r][u][d]==-1) continue; ans=max(ans,dp[r][u][d]); int up=max(ex-u,d+1),dn=min(ex+d,n-u); int le=max(ey-l,r+1),ri=min(ey+r,m-l); if (up>dn||le>ri) continue; if (u<U-d) mx(dp[r][u+1][d],dp[r][u][d]+sr[ex-u-1][ri]-sr[ex-u-1][le-1]); if (d<D-u) mx(dp[r][u][d+1],dp[r][u][d]+sr[ex+d+1][ri]-sr[ex+d+1][le-1]); if (r<R-l) mx(dp[r+1][u][d],dp[r][u][d]+sc[dn][ey+r+1]-sc[up-1][ey+r+1]); if (l<L-r) mx(dp[r][u][d] ,dp[r][u][d]+sc[dn][ey-l-1]-sc[up-1][ey-l-1]); } printf("%d",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef double db; #define go(u) for(int i = head[u], v = e[i].to; i; i=e[i].lst, v=e[i].to) #define rep(i, a, b) for(int i = a; i <= b; ++i) #define pb push_back #define re(x) memset(x, 0, sizeof x) inline int gi() { int x = 0,f = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar();} while(isdigit(ch)) { x = (x << 3) + (x << 1) + ch - 48; ch = getchar();} return x * f; } template <typename T> inline bool Max(T &a, T b){return a < b ? a = b, 1 : 0;} template <typename T> inline bool Min(T &a, T b){return a > b ? a = b, 1 : 0;} const int N = 107; int H, W, X, Y, ans; short f[N][N][N][N]; short s1[N][N], s2[N][N]; char s[N][N]; int main() { #ifdef fwat #endif H = gi(), W = gi(); rep(i, 1, H) { scanf("%s", s[i] + 1); rep(j, 1, W) if(s[i][j] == 'E') X = i, Y = j; } rep(i, 1, H) rep(j, 1, W) { s1[i][j] = s1[i - 1][j]; s2[i][j] = s2[i][j - 1]; if(s[i][j] == 'o') ++s1[i][j], ++s2[i][j]; } int xl = X - 1, xr = H - X, yl = Y, yr = W - Y; rep(a, 0, xl) { rep(b, 0, xr) rep(c, 0, yl) rep(d, 0, yr) { int L = max(Y - c, d + 1), R = min(Y + d, W - c); if(L <= R) { Max(f[a + 1][b][c][d], short(f[a][b][c][d] + (X - a - 1 <= b ? 0 : s2[X - a - 1][R] - s2[X - a - 1][L - 1]))); Max(f[a][b + 1][c][d], short(f[a][b][c][d] + (X + b + 1 > H - a ? 0 : s2[X + b + 1][R] - s2[X + b + 1][L - 1]))); } L = max(X - a, b + 1), R = min(X + b, H - a); if(L <= R) { Max(f[a][b][c + 1][d], short(f[a][b][c][d] + (Y - c - 1 <= d ? 0 : s1[R][Y - c - 1] - s1[L - 1][Y - c - 1]))); Max(f[a][b][c][d + 1], short(f[a][b][c][d] + (Y + d + 1 > W - c ? 0 : s1[R][Y + d + 1] - s1[L - 1][Y + d + 1]))); } } } printf("%d\n", f[xl][xr][yl][yr]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstdint> #include <algorithm> #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) using namespace std; template <class T> inline void setmax(T & a, T const & b) { a = max(a, b); } constexpr int max_h = 100; constexpr int max_w = 100; int16_t dp[max_h][max_w][max_h][max_w]; bool f[max_h][max_w]; int row_acc[max_h][max_w+1]; int col_acc[max_w][max_h+1]; int main() { int h, w; scanf("%d%d", &h, &w); int ey = -1, ex = -1; repeat (y,h) repeat (x,w) { char c; scanf(" %c", &c); if (c == 'o') f[y][x] = true; if (c == 'E') { ey = y; ex = x; } } repeat (y,h) repeat (x,w) row_acc[y][x+1] = row_acc[y][x] + f[y][x]; repeat (y,h) repeat (x,w) col_acc[x][y+1] = col_acc[x][y] + f[y][x]; repeat (ly,ey+1) { repeat (lx,ex+1) { repeat (ry,h-ey) { repeat (rx,w-ex) { auto col = [&](int x) { return rx <= x and x < w-lx ? col_acc[x][min(ey+ry+1,h-ly)] - col_acc[x][max(ey-ly,ry)] : 0; }; auto row = [&](int y) { return ry <= y and y < h-ly ? row_acc[y][min(ex+rx+1,w-lx)] - row_acc[y][max(ex-lx,rx)] : 0; }; if (ey-(ly+1) >= 0) setmax<int16_t>(dp[ly+1][lx][ry][rx], dp[ly][lx][ry][rx] + row(ey-(ly+1))); if (ex-(lx+1) >= 0) setmax<int16_t>(dp[ly][lx+1][ry][rx], dp[ly][lx][ry][rx] + col(ex-(lx+1))); if (ey+(ry+1) < h) setmax<int16_t>(dp[ly][lx][ry+1][rx], dp[ly][lx][ry][rx] + row(ey+(ry+1))); if (ex+(rx+1) < w) setmax<int16_t>(dp[ly][lx][ry][rx+1], dp[ly][lx][ry][rx] + col(ex+(rx+1))); } } } } printf("%d\n", dp[ey][ex][h-ey-1][w-ex-1]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<algorithm> #include<cstring> #define MN 105 using namespace std; int n,m,ex,ey,ans,sum[MN][MN],f[MN][MN][MN]; char ch[MN]; int C(int a,int b,int c,int d){return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];} int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",ch+1); for(int j=1;j<=m;j++)if(ch[j]=='o')sum[i][j]++;else if(ch[j]=='E')ex=i,ey=j; } for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]; int up,down,left,right;memset(f,-0x3f,sizeof(f));f[0][0][0]=0; for(int u=0;u<=n;u++){ if(ex-u<1)break; for(int d=0;d<=n;d++){ if(ex+d>n)break; up=max(ex-u,1+d); down=min(ex+d,n-u); if(up>down)continue; for(int l=0;l<=m;l++){ if(ey-l<1)break; for(int r=0;r<=m;r++){ if(ey+r>m)break; left=max(ey-l,r+1); right=min(ey+r,m-l); if(left>right)continue; ans=max(ans,f[d][l][r]); if(ex+d<n-u)f[d+1][l][r]=max(f[d+1][l][r],f[d][l][r]+C(ex+d+1,left,ex+d+1,right)); if(ey-l>r+1)f[d][l+1][r]=max(f[d][l+1][r],f[d][l][r]+C(up,ey-l-1,down,ey-l-1)); if(ey+r<m-l)f[d][l][r+1]=max(f[d][l][r+1],f[d][l][r]+C(up,ey+r+1,down,ey+r+1)); if(ex-u>d+1)f[d][l][r]=f[d][l][r]+C(ex-u-1,left,ex-u-1,right); } } } }printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x,y) x=min(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";} template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;} void chmax(short& x,short y){ if(x<y) x=y; } int H,W; string s[100]; short dp[100][100][100][100]; //u,b,l,r int o[101][101]; int onum(int u,int b,int l,int r){ //[u,b]*[l,r] b++,r++; return o[b][r]-o[u][r]-o[b][l]+o[u][l]; } int main(){ cin>>H>>W; rep(i,H) cin>>s[i]; rep(i,H) rep(j,W) if(s[i][j]=='o') o[i+1][j+1]=1; rep(i,H+1) rep(j,W) o[i][j+1]+=o[i][j]; rep(i,H) rep(j,W+1) o[i+1][j]+=o[i][j]; rep(i,H) rep(j,H) rep(k,W) rep(l,W) dp[i][j][k][l]=-1; int sx,sy; rep(i,H) rep(j,W) if(s[i][j]=='E') dp[i][i][j][j]=0,sx=i,sy=j; rep(dx,H) rep(dy,W) rep(u,H-dx) rep(l,W-dy){ int b=u+dx,r=l+dy; if(dp[u][b][l][r]<0) continue; // printf("u=%d,b=%d, l=%d,r=%d dp=%d\n",u,b,l,r,dp[u][b][l][r]); if(b-sx<u){ int L=max(l,r-sy),R=min(r,W-1-(sy-l)); chmax(dp[u-1][b][l][r],dp[u][b][l][r]+onum(u-1,u-1,L,R)); } if(H-1-(sx-u)>b){ int L=max(l,r-sy),R=min(r,W-1-(sy-l)); chmax(dp[u][b+1][l][r],dp[u][b][l][r]+onum(b+1,b+1,L,R)); } if(r-sy<l){ int U=max(u,b-sx),B=min(b,H-1-(sx-u)); chmax(dp[u][b][l-1][r],dp[u][b][l][r]+onum(U,B,l-1,l-1)); } if(W-1-(sy-l)>r){ int U=max(u,b-sx),B=min(b,H-1-(sx-u)); chmax(dp[u][b][l][r+1],dp[u][b][l][r]+onum(U,B,r+1,r+1)); } } short ans=0; rep(i,H) rep(j,H) rep(k,W) rep(l,W) chmax(ans,dp[i][j][k][l]); cout<<ans<<endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<cstring> #define maxn 105 #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) int n,m,sumr[maxn][maxn],sumc[maxn][maxn],ex,ey,ans; short dp[maxn][maxn][maxn][maxn]; char s[maxn][maxn]; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { sumr[i][j]=sumr[i][j-1]; sumc[i][j]=sumc[i-1][j]; if(s[i][j]=='E') ex=i,ey=j; if(s[i][j]=='o') sumr[i][j]++,sumc[i][j]++; } int L=ey-1,R=m-ey,U=ex-1,D=n-ex; memset(dp,-1,sizeof(dp)); dp[0][0][0][0]=0; for(int l=0;l<=L;l++) for(int r=0;r<=R;r++) for(int u=0;u<=U;u++) for(int d=0;d<=D;d++) { int tmp=dp[l][r][u][d]; if(tmp==-1) continue; ans=max(tmp,ans); int up=max(ex-u,d+1),dn=min(ex+d,n-u),le=max(ey-l,r+1),ri=min(ey+r,m-l); if(up>dn||le>ri) continue; if(u<U-d) dp[l][r][u+1][d]=max(dp[l][r][u+1][d],tmp+sumr[ex-u-1][ri]-sumr[ex-u-1][le-1]); if(d<D-u) dp[l][r][u][d+1]=max(dp[l][r][u][d+1],tmp+sumr[ex+d+1][ri]-sumr[ex+d+1][le-1]); if(r<R-l) dp[l][r+1][u][d]=max(dp[l][r+1][u][d],tmp+sumc[dn][ey+r+1]-sumc[up-1][ey+r+1]); if(l<L-r) dp[l+1][r][u][d]=max(dp[l+1][r][u][d],tmp+sumc[dn][ey-l-1]-sumc[up-1][ey-l-1]); } printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int inf=1e8; int h,w,s[110][110],sx,sy,f[2][110][110][110]; char a[110][110]; void clear(int t) { for (int u=0;u<sx;u++) { for (int r=0;r<=w-sy;r++) { for (int d=0;d<=h-sx;d++) { f[t][u][r][d]=-inf; } } } } int ask(int xa,int ya,int xb,int yb) { if (xa>xb || ya>yb) { return 0; } return s[xb][yb]+s[xa-1][ya-1]-s[xb][ya-1]-s[xa-1][yb]; } void upd(int &x,int v) { x=max(x,v); } int main() { scanf("%d%d",&h,&w); for (int i=1;i<=h;i++) { scanf("%s",a[i]+1); } for (int i=1;i<=h;i++) { for (int j=1;j<=w;j++) { s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]; if (a[i][j]=='o') { s[i][j]++; } if (a[i][j]=='E') { sx=i; sy=j; } } } int p=1; clear(p^1); f[p^1][0][0][0]=0; for (int l=0;l<sy;l++) { clear(p); p^=1; for (int u=0;u<sx;u++) { for (int r=0;r<=w-sy;r++) { for (int d=0;d<=h-sx;d++) { int v=f[p][u][r][d]; if (v<0) { continue; } int tmp; if (l+r+1<sy) { tmp=ask(max(d+1,sx-u),sy-l-1,min(h-u,sx+d),sy-l-1); } else { tmp=0; } upd(f[p^1][u][r][d],v+tmp); if (u+d+1<sx) { tmp=ask(sx-u-1,max(r+1,sy-l),sx-u-1,min(w-l,sy+r)); } else { tmp=0; } upd(f[p][u+1][r][d],v+tmp); if (l+r+sy<w) { tmp=ask(max(d+1,sx-u),sy+r+1,min(h-u,sx+d),sy+r+1); } else { tmp=0; } upd(f[p][u][r+1][d],v+tmp); if (u+d+sx<h) { tmp=ask(sx+d+1,max(r+1,sy-l),sx+d+1,min(w-l,sy+r)); } else { tmp=0; } upd(f[p][u][r][d+1],v+tmp); } } } } cout<<f[p][sx-1][w-sy][h-sx]<<endl; return 0; } //南有乔木,不可休思。 //——《国风·周南·汉广》
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstring> using namespace std; short f[105][105][105][105],sline[105][105],scol[105][105]; char st[105]; int n,m; void chkmax(short &a,short b){if(b>a)a=b;} int main() { scanf("%d%d",&n,&m);int X,Y; for(int i=1;i<=n;i++) { scanf("%s",st+1); for(int j=1;j<=m;j++) { if(st[j]=='E'){X=i,Y=j;} sline[i][j]=sline[i][j-1]+(st[j]=='o'),scol[i][j]=scol[i-1][j]+(st[j]=='o'); } } int mxl=Y-1,mxr=m-Y,mxu=X-1,mxd=n-X; for(int l=0;l<=mxl;l++) for(int r=0;r<=mxr;r++) for(int u=0;u<=mxu;u++) for(int d=0;d<=mxd;d++) { int L=max(d+1,X-u),R=min(X+d,n-u); short t=f[l][r][u][d]; if(L<=R) chkmax(f[l+1][r][u][d],t+(Y-l-1>r?scol[R][Y-l-1]-scol[L-1][Y-l-1]:0)), chkmax(f[l][r+1][u][d],t+(Y+r+1<=m-l?scol[R][Y+r+1]-scol[L-1][Y+r+1]:0)); L=max(r+1,Y-l),R=min(Y+r,m-l); if(L<=R) chkmax(f[l][r][u+1][d],t+(X-u-1>d?sline[X-u-1][R]-sline[X-u-1][L-1]:0)), chkmax(f[l][r][u][d+1],t+(X+d+1<=n-u?sline[X+d+1][R]-sline[X+d+1][L-1]:0)); } cout<<f[mxl][mxr][mxu][mxd]<<endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define list _list #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) using namespace std; const int N=105; short s[N][N],f[N][N][N][N],line[N][N],list[N][N]; int gc(){ char ch=getchar(); while(ch!='.'&&ch!='o'&&ch!='E') ch=getchar(); if (ch=='.') return 0; if (ch=='o') return 1; return 2; } int main(){ int n,m,t,x,y; cin>>n>>m; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j){ t=gc(); if (t==2) x=i,y=j; if (t==1) s[i][j]=1; } for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j){ line[i][j]=line[i][j-1]+s[i][j]; list[i][j]=list[i-1][j]+s[i][j]; } int ans=0,sum; for (int i=0; i<=n-x; ++i) for (int j=0; j<x; ++j) for (int k=0; k<=m-y; ++k) for (int l=0; l<y; ++l){ if (x+i+1<=n-j) sum=max(0,line[x+i+1][min(m-l,y+k)]-line[x+i+1][max(k,y-l-1)]); else sum=0; f[i+1][j][k][l]=max(f[i+1][j][k][l],f[i][j][k][l]+sum); if (x-j-1>i) sum=max(0,line[x-j-1][min(m-l,y+k)]-line[x-j-1][max(k,y-l-1)]); else sum=0; f[i][j+1][k][l]=max(f[i][j+1][k][l],f[i][j][k][l]+sum); if (y+k+1<=m-l) sum=max(0,list[min(n-j,x+i)][y+k+1]-list[max(i,x-j-1)][y+k+1]); else sum=0; f[i][j][k+1][l]=max(f[i][j][k+1][l],f[i][j][k][l]+sum); if (y-l-1>k) sum=max(0,list[min(n-j,x+i)][y-l-1]-list[max(i,x-j-1)][y-l-1]); else sum=0; f[i][j][k][l+1]=max(f[i][j][k][l+1],f[i][j][k][l]+sum); ans=max(ans,f[i][j][k][l]); } cout<<ans; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cmath> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <bitset> #include <set> const int maxlongint=2147483647; const int mo=1e9+7; const int N=105; using namespace std; short f[N][N][N][N],n,m,a[N][N],ans,sx,sy,v[N][N],v1[N][N]; int main() { cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char c=getchar(); while(c!='.' && c!='o' && c!='E') c=getchar(); v[i][j]=v1[i][j]=a[i][j]=(c=='o'); v[i][j]+=v[i][j-1]; v1[i][j]+=v1[i-1][j]; if(c=='E') sx=i,sy=j; } for(short i=sx;i>=1;i--) for(short j=sy;j>=1;j--) for(short i1=sx;i1<=n;i1++) for(short j1=sy;j1<=m;j1++) { if(1<i && i1+1<sx+i) f[i-1][j][i1][j1]=max((int)f[i-1][j][i1][j1],(int)f[i][j][i1][j1]+v[i-1][min((int)j1,m-sy+j)]-v[i-1][max((int)j-1,j1-sy)]); if(i1<n && sx+i1<n+i) f[i][j][i1+1][j1]=max((int)f[i][j][i1+1][j1],(int)f[i][j][i1][j1]+v[i1+1][min((int)j1,m-sy+j)]-v[i1+1][max((int)j-1,j1-sy)]); if(1<j && j1+1<sy+j) f[i][j-1][i1][j1]=max((int)f[i][j-1][i1][j1],(int)f[i][j][i1][j1]+v1[min((int)i1,n-sx+i)][j-1]-v1[max((int)i-1,i1-sx)][j-1]); if(j1<m && sy+j1<m+j) f[i][j][i1][j1+1]=max((int)f[i][j][i1][j1+1],(int)f[i][j][i1][j1]+v1[min((int)i1,n-sx+i)][j1+1]-v1[max((int)i-1,i1-sx)][j1+1]); } for(short i=sx;i>=1;i--) for(short j=sy;j>=1;j--) for(short i1=sx;i1<=n;i1++) for(short j1=sy;j1<=m;j1++) ans=max(f[i][j][i1][j1],ans); cout<<ans<<endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 102; short f[N][N][N][N] , s1[N][N] , s2[N][N]; char s[N][N]; int n , m; int main(){ cin >> n >> m; for(int i = 1 ; i <= n ; ++ i) cin >> (s[i] + 1); int px , py; for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) if(s[i][j] == 'E'){ px = i; py = j; break; } for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) s1[i][j] = s1[i][j - 1] + (short)(s[i][j] == 'o'); for(int i = 1 ; i <= n ; ++ i) for(int j = 1 ; j <= m ; ++ j) s2[i][j] = s2[i - 1][j] + (short)(s[i][j] == 'o'); memset(f , -1 , sizeof f); f[px][px][py][py] = 0; short ans = 0; for(int i = px ; i >= 1 ; -- i) for(int j = px ; j <= n ; ++ j) for(int k = py ; k >= 1 ; -- k) for(int t = py ; t <= m ; ++ t){ if(f[i][j][k][t] == -1) continue; if(i > 1 && i > j - px + 1) f[i - 1][j][k][t] = max(f[i - 1][j][k][t] , (short)(f[i][j][k][t] + s1[i - 1][min(m - py + k , t)] - s1[i - 1][max(k - 1 , t - py)])); if(j < n && j < n - (px - i)) f[i][j + 1][k][t] = max(f[i][j + 1][k][t] , (short)(f[i][j][k][t] + s1[j + 1][min(m - py + k , t)] - s1[j + 1][max(k - 1 , t - py)])); if(k > 1 && k > t - py + 1) f[i][j][k - 1][t] = max(f[i][j][k - 1][t] , (short)(f[i][j][k][t] + s2[min(n - px + i , j)][k - 1] - s2[max(i - 1 , j - px)][k - 1])); if(t < m && t < m - (py - k)) f[i][j][k][t + 1] = max(f[i][j][k][t + 1] , (short)(f[i][j][k][t] + s2[min(n - px + i , j)][t + 1] - s2[max(i - 1 , j - px)][t + 1])); ans = max(ans , f[i][j][k][t]); } cout << ans << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <cmath> using namespace std; const int MAXN = 100 + 5; int n, m; char gra[MAXN][MAXN]; short sum[MAXN][MAXN]; int ex, ey; short f[MAXN][MAXN][MAXN][MAXN]; int get_sum(int x1, int y1, int x2, int y2) { if(x1 > x2 || y1 > y2) return 0; return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]; } int dp(int x1, int y1, int x2, int y2) { if(x1 > x2 || y1 > y2) return 0; if(f[x1][y1][x2][y2] != -1) return f[x1][y1][x2][y2]; int ret = 0; // cut left ret = max(ret, dp(x1, y1 + 1, x2, y2) + (y1 + m - y2 < ey ? 0 : get_sum(max(x1, ex - n + x2), y1, min(x2, ex + x1 - 1), y1))); // cut right ret = max(ret, dp(x1, y1, x2, y2 - 1) + (y2 + 1 - y1 > ey ? 0 : get_sum(max(x1, ex - n + x2), y2, min(x2, ex + x1 - 1), y2))); // cut up ret = max(ret, dp(x1 + 1, y1, x2, y2) + (x1 + n - x2 < ex ? 0 : get_sum(x1, max(y1, ey - m + y2), x1, min(y2, ey + y1 - 1)))); // cut up ret = max(ret, dp(x1, y1, x2 - 1, y2) + (x2 + 1 - x1 > ex ? 0 : get_sum(x2, max(y1, ey - m + y2), x2, min(y2, ey + y1 - 1)))); // cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << ' ' << ret << endl; return f[x1][y1][x2][y2] = ret; } int main() { ios::sync_with_stdio(false); // freopen("1.in", "r", stdin); // freopen("1.out", "w", stdout); cin >> n >> m; for(int i = 1; i <= n; i++) cin >> (gra[i] + 1); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (gra[i][j] == 'o'); if(gra[i][j] == 'E') ex = i, ey = j; } // cout << sum[n][m] << endl; memset(f, -1, sizeof(f)); int ans = dp(1, 1, n, m); cout << ans << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include <set> #include <map> #include <algorithm> #define rep(i,a,b) for(int i = a; i <= b; i++) #define dep(i,a,b) for(int i = a; i >= b; i--) #define Rep(i,a) for(int i = 0; i < a; i++) using namespace std; typedef long long LL; typedef pair<int, int> pii; #define x first #define y second #define pb(a) push_back(a) const int N = 110; int n, m, f[2][N][N][N]; char a[N][N]; int s[N][N]; void upd(int &a, int b) { if (b > a) a = b; } int sum(int a, int b, int c, int d) { return s[b][d] - s[a - 1][d] - s[b][c - 1] + s[a - 1][c - 1]; } int main() { scanf("%d%d",&n,&m); rep(i,1,n) scanf("%s",a[i] + 1); int u, d, l, r; rep(i,1,n) rep(j,1,m) if (a[i][j] == 'E') { u = i, d = n - i + 1; l = j, r = m - j + 1; } rep(i,1,n) rep(j,1,m) s[i][j] = (a[i][j] == 'o'); rep(i,1,n) rep(j,1,m) s[i][j] += s[i][j - 1]; rep(i,1,n) rep(j,1,m) s[i][j] += s[i - 1][j]; int ans = 0; dep(i,n,1) { memset(f[i & 1], 0, sizeof(f[i & 1])); rep(j,i,n) dep(k,m,1) rep(t,k,m) { int F; //up F = f[(i & 1)^1][j][k][t]; if (j - i + 1 <= d) { int L = max(k, t - r + 1), R = min(t, k + l - 1); F += sum(i, i, L, R); } upd(f[i&1][j][k][t], F); //down F = f[i & 1][j - 1][k][t]; if (j - i + 1 <= u) { int L = max(k, t - r + 1), R = min(t, k + l - 1); F += sum(j, j, L, R); } upd(f[i&1][j][k][t], F); //left F = f[i & 1][j][k + 1][t]; if (t - k + 1 <= r) { int U = max(i, j - d + 1), D = min(j, i + u - 1); F += sum(U, D, k, k); } upd(f[i&1][j][k][t], F); //right F = f[i & 1][j][k][t - 1]; if (t - k + 1 <= l) { int U = max(i, j - d + 1), D = min(j, i + u - 1); F += sum(U, D, t, t); } upd(f[i&1][j][k][t], F); upd(ans, f[i & 1][j][k][t]); } } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; inline void upmax(short &x,const short &y){if(y>x) x=y;} const int N=105; short f[N][N][N][N]; short sum1[N][N],sum2[N][N]; int n,m,tx,ty; void gao() { int limu=tx-1,limd=n-tx,liml=ty-1,limr=m-ty; for(int u=0;u<=limu;u++) for(int d=0;d<=limd;d++) for(int l=0;l<=liml;l++) for(int r=0;r<=limr;r++) { int L=max(ty-l,r+1),R=min(ty+r,m-l); if(L<=R) { upmax(f[u+1][d][l][r],f[u][d][l][r]+(tx-u-1>=d+1?sum1[tx-u-1][R]-sum1[tx-u-1][L-1]:0)); upmax(f[u][d+1][l][r],f[u][d][l][r]+(tx+d+1<=n-u?sum1[tx+d+1][R]-sum1[tx+d+1][L-1]:0)); } L=max(tx-u,d+1),R=min(tx+d,n-u); if(L<=R) { upmax(f[u][d][l+1][r],f[u][d][l][r]+(ty-l-1>=r+1?sum2[R][ty-l-1]-sum2[L-1][ty-l-1]:0)); upmax(f[u][d][l][r+1],f[u][d][l][r]+(ty+r+1<=m-l?sum2[R][ty+r+1]-sum2[L-1][ty+r+1]:0)); } } printf("%d\n",f[limu][limd][liml][limr]); } int main() { static char s[105]; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=m;j++) { if(s[j]=='E') tx=i,ty=j; sum1[i][j]=sum1[i][j-1]+(s[j]=='o'); sum2[i][j]=sum2[i-1][j]+(s[j]=='o'); } } gao(); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int N=105; int n,m,x,y,s1[N][N],s2[N][N]; char s[N][N]; short dp[N][N][N][N]; template<class T>void up(T&x,int y){x>y?:x=y;} inline int GetL(int Li,int L,int R){return s1[Li][R]-s1[Li][L-1];} inline int GetC(int Co,int L,int R){return s2[Co][R]-s2[Co][L-1];} int main(){ // freopen("agc004_e.in","r",stdin);freopen("agc004_e.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",s[i]+1); for(int j=1;j<=m;j++){ if(s[i][j]=='E')x=i,y=j; s1[i][j]=s1[i][j-1]+(s[i][j]=='o'); s2[j][i]=s2[j][i-1]+(s[i][j]=='o'); } } for(int l=0;l<y;l++)for(int r=0;r<m-y+1;r++){ for(int u=0;u<x;u++)for(int d=0;d<n-x+1;d++){ int now=dp[l][r][u][d]; int lx1=d+1,rx1=n-u; int ly1=r+1,ry1=m-l; // inside the boundry int lx2=x-u-1,rx2=x+d+1; int ly2=y-l-1,ry2=y+r+1; // outside the boundry int LX = max(lx1, lx2 + 1), RX = min(rx1, rx2 - 1); int LY = max(ly1, ly2 + 1), RY = min(ry1, ry2 - 1); up (dp[l + 1][r][u][d], now + (ly2 < ly1 ? 0 : GetC (ly2, LX, RX))); up (dp[l][r + 1][u][d], now + (ry2 > ry1 ? 0 : GetC (ry2, LX, RX))); up (dp[l][r][u + 1][d], now + (lx2 < lx1 ? 0 : GetL (lx2, LY, RY))); up (dp[l][r][u][d + 1], now + (rx2 > rx1 ? 0 : GetL (rx2, LY, RY))); } } printf("%d\n",(int)dp[y-1][m-y][x-1][n-x]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; const int DIM = 1e2 + 5; char arr[DIM][DIM]; int dp[DIM][DIM][DIM][DIM], psm[DIM][DIM]; inline int sum( int x1, int y1, int x2, int y2 ) { return psm[x2][y2] - psm[x1 - 1][y2] - psm[x2][y1 - 1] + psm[x1 - 1][y1 - 1]; } int main( void ) { int n, m; cin >> n >> m; int px = 0, py = 0; for( int i = 1; i <= n; i ++ ) { cin >> ( arr[i] + 1 ); for( int j = 1; j <= m; j ++ ) { psm[i][j] = psm[i - 1][j] + psm[i][j - 1] - psm[i - 1][j - 1] + ( arr[i][j] == 'o' ); if( arr[i][j] == 'E' ) { px = i; py = j; } } } for( int l = 0; l <= py - 1; l ++ ) { for( int r = 0; r <= m - py; r ++ ) { for( int u = 0; u <= px - 1; u ++ ) { for( int d = 0; d <= n - px; d ++ ) { dp[l + 1][r][u][d] = max( dp[l + 1][r][u][d], dp[l][r][u][d] + ( ( r + 1 <= py - l - 1 ) ? sum( max( px - u, d + 1 ), py - l - 1, min( px + d, n - u ), py - l - 1 ) : 0 ) ); dp[l][r + 1][u][d] = max( dp[l][r + 1][u][d], dp[l][r][u][d] + ( ( py + r + 1 <= m - l ) ? sum( max( px - u, d + 1 ), py + r + 1, min( px + d, n - u ), py + r + 1 ) : 0 ) ); dp[l][r][u + 1][d] = max( dp[l][r][u + 1][d], dp[l][r][u][d] + ( ( d + 1 <= px - u - 1 ) ? sum( px - u - 1, max( py - l, r + 1 ), px - u - 1, min( py + r, m - l ) ) : 0 ) ); dp[l][r][u][d + 1] = max( dp[l][r][u][d + 1], dp[l][r][u][d] + ( ( px + d + 1 <= n - u ) ? sum( px + d + 1, max( py - l, r + 1 ), px + d + 1, min( py + r, m - l ) ) : 0 ) ); } } } } cout << dp[py - 1][m - py][px - 1][n - px] << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() const int M = 101; int H, W; int sx, sy; short ac[M][M]; string s[M]; short dp[M][M][M][M]; short get(int a, int b, int c, int d) { //[a,c],[b,d] if (a > c || b > d) return 0; return ac[c+1][d+1] - ac[c+1][b] - ac[a][d+1] + ac[a][b]; } short calc(int p, int q, int r, int s) {//dead : bottom p, top r, right r, left s if (dp[p][q][r][s] != -1) { return dp[p][q][r][s]; } short res = 0; if (p + r < H) { if (sx - p - 1 >= r) { res = max<short>(res, calc(p + 1, q, r, s) + get(sx - p - 1, max(sy - q, s), sx - p - 1, min(sy + s, W - 1 - q))); } if (sx + r + 1 < H - p) { res = max<short>(res, calc(p, q, r + 1, s) + get(sx + r + 1, max(sy - q, s), sx + r + 1, min(sy + s, W - 1 - q))); } } if (q + s < W) { if (sy - q - 1 >= s) { res = max<short>(res, calc(p, q + 1, r, s) + get(max(sx - p, r), sy - q - 1, min(sx + r, H - 1 - p), sy - q - 1)); } if (sy + s + 1 < W - q) { res = max<short>(res, calc(p, q, r, s + 1) + get(max(sx - p, r), sy + s + 1, min(sx + r, H - 1 - p), sy + s + 1)); } } return dp[p][q][r][s] = res; } int main() { cin >> H >> W; rep(i, H) cin >> s[i]; rep(i, H) { rep(j, W) { ac[i+1][j+1] = ac[i+1][j] + ac[i][j+1] - ac[i][j] + (s[i][j] == 'o'); if (s[i][j] == 'E') { sx = i, sy = j; } } } memset(dp, -1, sizeof(dp)); cout << calc(0, 0, 0, 0) << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define pb push_back typedef double db; typedef long long ll; typedef pair<int,int> P; const int MAXN=101; char s[MAXN][MAXN]; int n,m,x,y; short dp[MAXN][MAXN][MAXN][MAXN]; short line[MAXN][MAXN],col[MAXN][MAXN]; void upd(short &a,short b){a=max(a,b);} int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",s[i]+1); for(int j=1;j<=m;j++) { if(s[i][j]=='E') x=i,y=j; line[i][j]=line[i][j-1]+(s[i][j]=='o'); col[i][j]=col[i-1][j]+(s[i][j]=='o'); } } short res=0; dp[0][0][0][0]=0; for(int u=0;u<x;u++) for(int d=0;d<=n-x;d++) for(int l=0;l<y;l++) for(int r=0;r<=m-y;r++) { if(!(u+d<max(x,n-x+1)&&l+r<max(y,m-y+1))) continue; short &cur=dp[u][d][l][r];res=max(res,cur); int L=max(r+1,y-l),R=min(y+r,m-l),D=min(x+d,n-u),U=max(x-u,d+1); if(u+1+d<x) upd(dp[u+1][d][l][r],cur+line[x-u-1][R]-line[x-u-1][L-1]); if(d+1+u<=n-x) upd(dp[u][d+1][l][r],cur+line[x+d+1][R]-line[x+d+1][L-1]); if(l+1+r<y) upd(dp[u][d][l+1][r],cur+col[D][y-l-1]-col[U-1][y-l-1]); if(r+1+l<=m-y) upd(dp[u][d][l][r+1],cur+col[D][y+r+1]-col[U-1][y+r+1]); } printf("%d",res); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; #define y1 ___y1 const int N=105; short dp[N][N][N][N],p; char s[N][N]; int a[N][N],n,m,x,y; int get(int x1,int y1,int x2,int y2){ if (x1>x2||y1>y2)return 0; return a[x2][y2]-a[x2][y1-1]-a[x1-1][y2]+a[x1-1][y1-1]; } int Max(int x,int y){ if (x>y)return x; return y; } int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++)scanf("%s",s[i]+1); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ if (s[i][j]=='o')a[i][j]=1; else if (s[i][j]=='E')x=i,y=j; a[i][j]+=a[i-1][j]; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++)a[i][j]+=a[i][j-1]; for (int d=0;d<=n-x;d++) for (int u=0;u<x;u++) for (int l=0;l<y;l++) for (int r=0;r<=m-y;r++){ if (d!=n-x)dp[d+1][u][l][r]=Max(dp[d+1][u][l][r],dp[d][u][l][r]+ (x+d+1>n-u?p:get(x+d+1,max(r+1,y-l),x+d+1,min(m-l,y+r)))); if (u!=x-1)dp[d][u+1][l][r]=Max(dp[d][u+1][l][r],dp[d][u][l][r]+ (x-u-1<=d?p:get(x-u-1,max(r+1,y-l),x-u-1,min(m-l,y+r)))); if (l!=y-1)dp[d][u][l+1][r]=Max(dp[d][u][l+1][r],dp[d][u][l][r]+ (y-l-1<=r?p:get(max(d+1,x-u),y-l-1,min(n-u,x+d),y-l-1))); if (r!=m-y)dp[d][u][l][r+1]=Max(dp[d][u][l][r+1],dp[d][u][l][r]+ (y+r+1>m-l?p:get(max(d+1,x-u),y+r+1,min(n-u,x+d),y+r+1))); } cout<<dp[n-x][x-1][y-1][m-y]; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAX_N = 105; int f[MAX_N][MAX_N][MAX_N], g[MAX_N][MAX_N][MAX_N]; int H, W, A[MAX_N][MAX_N], eX, eY; inline void update(int &x, int y) { if (x < y) x = y; } inline int get_num(int a, int b, int c, int d) { if (a > c || b > d) return 0; return A[c][d] - A[a - 1][d] - A[c][b - 1] + A[a - 1][b - 1]; } int main() { scanf("%d%d", &H, &W); for (int i = 1; i <= H; ++i) { static char S[MAX_N]; scanf("%s", S + 1); for (int j = 1; j <= W; ++j) { A[i][j] = A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1] + (S[j] == 'o'); if (S[j] == 'E') eX = i, eY = j; } } f[0][0][0] = 0; int res = 0; for (int i = 0; i < H; ++i) { memset(g, 0, sizeof g); for (int j = 0; j + i <= H; ++j) for (int k = 0; k < W; ++k) for (int m = 0; m + k <= W; ++m) { res = max(res, f[j][k][m]); int U = i + 1, D = H - j, L = k + 1, R = W - m; int u = eX - j, d = eX + i, l = eY - m, r = eY + k; if (U > D || L > R) continue; if (d + 1 <= D && d + 1 >= U) update(g[j][k][m], f[j][k][m] + get_num(d + 1, max(L, l), d + 1, min(R, r))); if (u - 1 >= U && u - 1 <= D) update(f[j + 1][k][m], f[j][k][m] + get_num(u - 1, max(L, l), u - 1, min(R, r))); if (r + 1 <= R && r + 1 >= L) update(f[j][k + 1][m], f[j][k][m] + get_num(max(U, u), r + 1, min(D, d), r + 1)); if (l - 1 >= L && l - 1 <= R) update(f[j][k][m + 1], f[j][k][m] + get_num(max(U, u), l - 1, min(D, d), l - 1)); } memcpy(f, g, sizeof f); } printf("%d\n" , res); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<algorithm> #include<cstdio> #include<cstring> using namespace std; #define MAXN 105 int dp[MAXN][MAXN][MAXN],N,M,SumR[MAXN][MAXN],SumC[MAXN][MAXN],sx,sy,ans; char s[MAXN]; int main() { memset(dp,-1,sizeof dp); scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) { scanf("%s",s+1); for(int j=1;j<=M;j++) { SumR[i][j]=SumR[i][j-1]; SumC[i][j]=SumC[i-1][j]; if(s[j]=='E') sx=i,sy=j; else if(s[j]=='o') SumR[i][j]++,SumC[i][j]++; } } dp[0][0][0]=0; int L=sy-1,R=M-sy,U=sx-1,D=N-sx; for(int l=0;l<=L;l++) for(int r=0;r<=R;r++) for(int u=0;u<=U;u++) for(int d=0;d<=D;d++) { if(dp[r][u][d]==-1) continue; ans=max(ans,dp[r][u][d]); int up=max(d,sx-u-1); int down=min(N-u,sx+d); int le=max(r,sy-l-1); int ri=min(M-l,sy+r); if(up>=down||le>=ri) continue; if(u<U-d) dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+SumR[sx-u-1][ri]-SumR[sx-u-1][le]); if(d<D-u) dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+SumR[sx+d+1][ri]-SumR[sx+d+1][le]); if(r<R-l) dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+SumC[down][sy+r+1]-SumC[up][sy+r+1]); if(l<L-r) dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+SumC[down][sy-l-1]-SumC[up][sy-l-1]); } printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<vector> using namespace std; #define LL long long #define DB double #define MAXN 100 #define MOD 1000000007 #define Pr pair<int,int> #define X first #define Y second #define INF 1000000000000000000 #define mem(x,v) memset(x,v,sizeof(x)) LL read(){ LL x=0,F=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*F; } int add(int a,int b){return (a+b>=MOD)?a+b-MOD:a+b;} int dec(int a,int b){return (a-b<0)?a-b+MOD:a-b;} int mul(int a,int b){return (1LL*a*b)%MOD;} int fst_pow(int a,int b){ int res=1; while(b){ if(b&1)res=mul(res,a); a=mul(a,a),b>>=1; } return res; } int n,m; int sx,sy; char s[MAXN+5][MAXN+5]; short dp[MAXN+5][MAXN+5][MAXN+5][MAXN+5],a[MAXN+5][MAXN+5],b[MAXN+5][MAXN+5]; void upd(short &x,short y){x=max(x,y);} int main(){ n=read(),m=read(); for(int i=1;i<=n;i++){ scanf("%s",s[i]+1); for(int j=1;j<=m;j++){ if(s[i][j]=='E')sx=i,sy=j; a[i][j]=a[i][j-1]+(s[i][j]=='o'); b[i][j]=b[i-1][j]+(s[i][j]=='o'); } } int L=sy-1,U=sx-1,R=m-sy,D=n-sx; for(int u=0;u<=U;u++) for(int d=0;d<=D;d++) for(int l=0;l<=L;l++) for(int r=0;r<=R;r++){ int pl=max(sy-l,r+1),pr=min(sy+r,m-l); if(pl<=pr){ upd(dp[u+1][d][l][r],dp[u][d][l][r]+((sx-u-1>=d+1)?a[sx-u-1][pr]-a[sx-u-1][pl-1]:0)); upd(dp[u][d+1][l][r],dp[u][d][l][r]+((sx+d+1<=n-u)?a[sx+d+1][pr]-a[sx+d+1][pl-1]:0)); } pl=max(sx-u,d+1),pr=min(sx+d,n-u); if(pl<=pr){ upd(dp[u][d][l+1][r],dp[u][d][l][r]+((sy-l-1>=r+1)?b[pr][sy-l-1]-b[pl-1][sy-l-1]:0)); upd(dp[u][d][l][r+1],dp[u][d][l][r]+((sy+r+1<=m-l)?b[pr][sy+r+1]-b[pl-1][sy+r+1]:0)); } } printf("%d\n",dp[U][D][L][R]); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; short dp[MAXN][MAXN][MAXN][MAXN], f[MAXN][MAXN], g[MAXN][MAXN], n, m; char str[MAXN]; inline void update(short &a, short b) { a = max(a, b); } inline short min(int a, short b) { return min((short)a, b);} int main() { cin >> n >> m; short x, y; for (int i = 1; i <= n; i++) { cin >> str+1; for (int j = 1; j <= m; j++) { if (str[j] == 'E') x = i, y = j; else if (str[j] == 'o') f[i][j] = g[j][i] = 1; } } short lenx = max(x-1, n-x), leny = max(y-1, m-y); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) f[i][j] += f[i][j-1]; for (int j = 1; j <= m; j++) for (int i = 1; i <= n; i++) g[j][i] += g[j][i-1]; short ans = 0; for (short i = x; i >= 1; i--) for (short j = x; j <= n && j-i <= lenx; j++) for (short k = y; k >= 1; k--) for (short w = y; w <= m && w-k <= leny; w++) { short R = min(m+k-y, w), L = max(k-1, w-y); if (L < R) { if (i-1 >= 1 && j-i+1 <= x-1) update(dp[i-1][j][k][w], dp[i][j][k][w]+f[i-1][R]-f[i-1][L]); if (j+1 <= n && j-i+1 <= n-x) update(dp[i][j+1][k][w], dp[i][j][k][w]+f[j+1][R]-f[j+1][L]); } R = min(n+i-x, j), L = max(i-(short)1, j-x); if (L < R) { if (k-1 >= 1 && w-k+1 <= y-1) update(dp[i][j][k-1][w], dp[i][j][k][w]+g[k-1][R]-g[k-1][L]); if (w+1 <= m && w-k+1 <= m-y) update(dp[i][j][k][w+1], dp[i][j][k][w]+g[w+1][R]-g[w+1][L]); } ans = max(ans, dp[i][j][k][w]); } cout << ans << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N=105; short f[N][N][N][N],sum1[N][N],sum2[N][N];char a[N][N]; inline void ckmax(short&x,short y){ x=(x<y?y:x); } int main (){ int n,m;scanf ("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf ("%s",a[i]+1); int tx,ty; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ if (a[i][j]=='E') tx=i,ty=j; sum1[i][j]=sum1[i][j-1]+(a[i][j]=='o'); sum2[i][j]=sum2[i-1][j]+(a[i][j]=='o'); } int limi=tx-1,limj=n-tx,limk=ty-1,liml=m-ty; for (int i=0;i<=limi;i++) for (int j=0;j<=limj;j++) for (int k=0;k<=limk;k++) for (int l=0;l<=liml;l++){ int L=max(ty-k,l+1),R=min(ty+l,m-k); if (L<=R){ ckmax(f[i+1][j][k][l],f[i][j][k][l]+(tx-i-1>=j+1?sum1[tx-i-1][R]-sum1[tx-i-1][L-1]:0)); ckmax(f[i][j+1][k][l],f[i][j][k][l]+(tx+j+1<=n-i?sum1[tx+j+1][R]-sum1[tx+j+1][L-1]:0)); } L=max(tx-i,j+1),R=min(tx+j,n-i); if (L<=R){ ckmax(f[i][j][k+1][l],f[i][j][k][l]+(ty-k-1>=l+1?sum2[R][ty-k-1]-sum2[L-1][ty-k-1]:0)); ckmax(f[i][j][k][l+1],f[i][j][k][l]+(ty+l+1<=m-k?sum2[R][ty+l+1]-sum2[L-1][ty+l+1]:0)); } } printf ("%d",f[limi][limj][limk][liml]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> const int N=101; short f[N][N][N][N]; short pl[N][N],pu[N][N]; char s[N][N]; short n,m,sx,sy,ans; inline short min(short A,short B) {return A<B?A:B;} inline short max(short A,short B) {return A>B?A:B;} inline void cmax(short&A,short B) {A=A>B?A:B;} void init();void work(); int main(){init();work();return 0;} void work(){ for(short l=sy;l;--l) for(short u=sx;u;--u) for(short r=sy;r<=m;++r) for(short d=sx;d<=n;++d){ if(1 < u and d+1 < sx+u) cmax(f[u-1][l][d][r] , f[u][l][d][r] + pl[u-1][min(r,m-(sy-l))]-pl[u-1][max(l,r-sy+1)-1]); if(1 < l and r+1 < sy+l) cmax(f[u][l-1][d][r] , f[u][l][d][r] + pu[l-1][min(d,n-(sx-u))]-pu[l-1][max(u,d-sx+1)-1]); if(d < n and sx+d < n+u) cmax(f[u][l][d+1][r] , f[u][l][d][r] + pl[d+1][min(r,m-(sy-l))]-pl[d+1][max(l,r-sy+1)-1]); if(r < m and sy+r < m+l) cmax(f[u][l][d][r+1] , f[u][l][d][r] + pu[r+1][min(d,n-(sx-u))]-pu[r+1][max(u,d-sx+1)-1]); cmax(ans,f[u][l][d][r]); } printf("%d\n",(int)ans); } void init(){ int inn,inm; scanf("%d%d",&inn,&inm); n=inn,m=inm; for(int i=1;i<=n;++i){ scanf("%s",s[i]+1); for(int j=1;j<=m;++j){ pl[i][j]=pl[i][j-1]+(s[i][j]=='o'); if(s[i][j]=='E')s[i][j]='.',sx=i,sy=j; } } for(int j=1;j<=m;++j) for(int i=1;i<=n;++i) pu[j][i]=pu[j][i-1]+(s[i][j]=='o'); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> using namespace std; char map[105][105]; int n,m,x,y,ans=0,a[105][105],b[105][105]; short f[105][105][105][105]; int max(int x,int y){return x>y?x:y;} int min(int x,int y){return x<y?x:y;} int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",map[i]+1); for(int j=1;j<=m;j++){ a[i][j]=a[i][j-1]+(map[i][j]=='o'); b[i][j]=b[i-1][j]+(map[i][j]=='o'); if(map[i][j]=='E')x=i,y=j; } } for(int i=x;i>=1;i--) for(int j=y;j>=1;j--){ for(int k=x;k<=n;k++) for(int l=y;l<=m;l++){ if(i>1&&i-1>k-x)ans=max(ans,f[i-1][j][k][l]=max(f[i-1][j][k][l],f[i][j][k][l]+a[i-1][min(l,m-y+j)]-a[i-1][max(j-1,l-y)])); if(k<n&&n-k>x-i)ans=max(ans,f[i][j][k+1][l]=max(f[i][j][k+1][l],f[i][j][k][l]+a[k+1][min(l,m-y+j)]-a[k+1][max(j-1,l-y)])); if(j>1&&j-1>l-y)ans=max(ans,f[i][j-1][k][l]=max(f[i][j-1][k][l],f[i][j][k][l]+b[min(k,n-x+i)][j-1]-b[max(i-1,k-x)][j-1])); if(l<m&&m-l>y-j)ans=max(ans,f[i][j][k][l+1]=max(f[i][j][k][l+1],f[i][j][k][l]+b[min(k,n-x+i)][l+1]-b[max(i-1,k-x)][l+1])); } } printf("%d",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define res register short #define N 105 using namespace std; short line[N][N],List[N][N],ans[N][N][N][N],n,m,op,sx,sy; char s[N][N]; signed main() { cin>>n>>m; for (res i=1; i<=n; i++) scanf("%s",s[i]+1); for (res i=1; i<=n; i++) for (res j=1; j<=m; j++) { line[i][j]=line[i][j-1]+(s[i][j]=='o'); List[i][j]=List[i-1][j]+(s[i][j]=='o'); if (s[i][j]=='E') sx=i,sy=j; } for (res i=sx; i; i--) for (res j=sy; j; j--) for (res k=sx; k<=n; k++) for (res p=sy; p<=m; p++) { if (1<i && k+1<sx+i) op=max(op,ans[i-1][j][k][p]=max((int)ans[i-1][j][k][p],ans[i][j][k][p]+line[i-1][min((int)p,m-sy+j)]-line[i-1][max(j-1,p-sy)])); if (k<n && sx+k<n+i) op=max(op,ans[i][j][k+1][p]=max((int)ans[i][j][k+1][p],ans[i][j][k][p]+line[k+1][min((int)p,m-sy+j)]-line[k+1][max(j-1,p-sy)])); if (1<j && p+1<sy+j) op=max(op,ans[i][j-1][k][p]=max((int)ans[i][j-1][k][p],ans[i][j][k][p]+List[min((int)k,n-sx+i)][j-1]-List[max(i-1,k-sx)][j-1])); if (p<m && sy+p<m+j) op=max(op,ans[i][j][k][p+1]=max((int)ans[i][j][k][p+1],ans[i][j][k][p]+List[min((int)k,n-sx+i)][p+1]-List[max(i-1,k-sx)][p+1])); } cout<<op<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int N=109; int n,m,x,y; char g[N][N]; int f[2][N][N][N]; int row[N][N],col[N][N]; inline void chkmax(int &a,int b){if(a<b)a=b;} int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",g[i]+1); for(int j=1;j<=m;j++) { col[j][i]=col[j][i-1]; row[i][j]=row[i][j-1]; if(g[i][j]=='E') x=i,y=j; else if(g[i][j]=='o') row[i][j]++,col[j][i]++; } } for(int i=0;i<x;i++) { int fl=i&1; memset(f[fl],0,sizeof(f[fl])); for(int j=0;j<=n-x;j++) for(int k=0;k<y;k++) for(int l=0;l<=m-y;l++) { chkmax(f[fl][j][k][l],f[fl^1][j][k][l]+(x-i-1>j?row[x-i-1][min(y+l,m-k)]-row[x-i-1][max(y-k-1,l)]:0)); chkmax(f[fl^1][j+1][k][l],f[fl^1][j][k][l]+(x+j+1<=n-i?row[x+j+1][min(y+l,m-k)]-row[x+j+1][max(y-k-1,l)]:0)); chkmax(f[fl^1][j][k+1][l],f[fl^1][j][k][l]+(y-k-1>l?col[y-k-1][min(x+j,n-i)]-col[y-k-1][max(x-i-1,j)]:0)); chkmax(f[fl^1][j][k][l+1],f[fl^1][j][k][l]+(y+l+1<=m-k?col[y+l+1][min(x+j,n-i)]-col[y+l+1][max(x-i-1,j)]:0)); } } printf("%d\n",f[x-1&1][n-x][y-1][m-y]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) #define eprintf(s...) fprintf(stderr, s) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } int H, W; char F[111][111]; int A[101][101]; short dp[101][101][101][101]; short box(int l, int u, int r, int d) { return A[d][r] - A[d][l] - A[u][r] + A[u][l]; } int main() { scanf("%d%d", &H, &W); REP (i, H) scanf("%s", F[i]); int si, sj; REP (i, H) REP (j, W) { if (F[i][j] == 'E') { si = i; sj = j; } A[i+1][j+1] = A[i][j+1] + A[i+1][j] - A[i][j]; if (F[i][j] == 'o') A[i+1][j+1] ++; } int ans = 0; REP (l, sj+1) REP (r, W-sj) REP (u, si+1) REP (d, H-si) { short cur = dp[l][r][u][d]; amax(ans, (int)cur); int R, L, D, U; U = max(si-u, d); D = min(si+d+1, H-u); if (sj-1-l >= r && U < D) amax<short>(dp[l+1][r][u][d], cur + box(sj-1-l, U, sj-l, D)); if (sj+1+r < W-l && U < D) amax<short>(dp[l][r+1][u][d], cur + box(sj+1+r, U, sj+2+r, D)); L = max(sj-l, r); R = min(sj+r+1, W-l); if (si-1-u >= d && L < R) amax<short>(dp[l][r][u+1][d], cur + box(L, si-1-u, R, si-u)); if (si+1+d < H-u && L < R) amax<short>(dp[l][r][u][d+1], cur + box(L, si+1+d, R, si+2+d)); } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define N 105 using namespace std; char s[N][N]; int n,m; int Ex,Ey,ans; int cnt[N][N]; void mmax(int &x,int v){ x=max(x,v);ans=max(ans,v); } int get(int x1,int y1,int x2,int y2){ return cnt[x2][y2]-cnt[x2][y1-1]-cnt[x1-1][y2]+cnt[x1-1][y1-1]; } int dp[2][N][N][N]; int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%s",s[i]+1); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ cnt[i][j]=cnt[i-1][j]+cnt[i][j-1]-cnt[i-1][j-1]+(s[i][j]=='o'); if(s[i][j]=='E') Ex=i,Ey=j; } int now=0; for (int i=0;i<=Ex-1;i++){ memset(dp[now^1],0,sizeof(dp[0])); for (int j=0;j<=n-Ex;j++) for (int k=0;k<=Ey-1;k++) for (int L=0;L<=m-Ey;L++){ int v=dp[now][j][k][L]; int xl=max(Ex-i,j+1),yl=max(Ey-k,L+1),xr=min(Ex+j,n-i),yr=min(Ey+L,m-k); if (Ex-i-1>j) mmax(dp[now^1][j][k][L],v+get(Ex-i-1,yl,Ex-i-1,yr)); if (Ex+j<n-i) mmax(dp[now][j+1][k][L],v+get(Ex+j+1,yl,Ex+j+1,yr)); if (Ey-k-1>L) mmax(dp[now][j][k+1][L],v+get(xl,Ey-k-1,xr,Ey-k-1)); if (Ey+L<m-k) mmax(dp[now][j][k][L+1],v+get(xl,Ey+L+1,xr,Ey+L+1)); } now^=1; } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> using namespace std; int main() { int h,w; scanf("%d%d",&h,&w); char M[101][101]; for(int i=0;i<h;i++){ scanf("%s",M[i]); } int x,y; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(M[i][j]=='E'){ x=j; y=i; } } } int x1=x+1,y1=y+1,x2=w-x,y2=h-y; static int R[100][101],C[100][101]; for(int i=0;i<h;i++){ R[i][0]=0; for(int j=0;j<w;j++){ R[i][j+1]=R[i][j]+(M[i][j]=='o'?1:0); } } for(int j=0;j<w;j++){ C[j][0]=0; for(int i=0;i<h;i++){ C[j][i+1]=C[j][i]+(M[i][j]=='o'?1:0); } } static int dp[101][101][101][101]; for(int H=0;H<=h;H++){ for(int a=0;a+H<=h;a++){ int b=a+H; for(int W=0;W<=w;W++){ for(int c=0;c+W<=w;c++){ int d=c+W; if(a==b||c==d){//||!(max(a,b-y2)<=y&&y<min(b,a+x1)&&max(c,d-x2)<=x&&x<min(d,c+x1))){ dp[a][b][c][d]=0; continue; } dp[a][b][c][d]=max(max(dp[a+1][b][c][d]+(b-a>y2?0:-R[a][max(c,d-x2)]+R[a][min(d,c+x1)]), dp[a][b-1][c][d]+(b-a>y1?0:-R[b-1][max(c,d-x2)]+R[b-1][min(d,c+x1)])), max(dp[a][b][c+1][d]+(d-c>x2?0:-C[c][max(a,b-y2)]+C[c][min(b,a+y1)]), dp[a][b][c][d-1]+(d-c>x1?0:-C[d-1][max(a,b-y2)]+C[d-1][min(b,a+y1)]))); //printf("%d %d %d %d %d %d %d %d %d\n",a,b,c,d,dp[a][b][c][d],max(c,d-x2),min(d,c+x1),R[a][max(c,d-x2)],R[a][min(d,c+x1)]); } } } } printf("%d\n",dp[0][h][0][w]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; int H, W, rlen, clen, psum[102][102]; char G[102][102]; int calc(int r1, int c1, int r2, int c2) { int ret = psum[r2][c2]; if(r1) ret -= psum[r1 - 1][c2]; if(c1) ret -= psum[r2][c1 - 1]; if(r1 && c1) ret += psum[r1 - 1][c1 - 1]; return ret; } int re, ce; int cc[102][102]; int dp(int r, int c) { int &ret = cc[r][c]; if(ret != -1) return ret; ret = 0; if(r + rlen - 1 != re) { ret = max(ret, calc(r + rlen, c, r + rlen, c + clen - 1) + dp(r + 1, c)); } if(c + clen - 1 != ce) { ret = max(ret, calc(r, c + clen, r + rlen - 1, c + clen) + dp(r, c + 1)); } return ret; } int main() { scanf("%d %d", &H, &W); int r, c; for(int i = 0; i < H; i++) { scanf("\n"); for(int j = 0; j < W; j++) { scanf("%c", &G[i][j]); if(G[i][j] == 'E') r = i, c = j; } } if(r + 1 > H - r) { for(int i = 0; i < H - 1 - i; i++) for(int j = 0; j < W; j++) swap(G[i][j], G[H - 1 - i][j]); r = H - 1 - r; } if(c + 1 > W - c) { for(int i = 0; i < H; i++) for(int j = 0; j < W - 1 - j; j++) swap(G[i][j], G[i][W - 1 - j]); c = W - 1 - c; } rlen = r + 1; clen = c + 1; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { psum[i][j] = G[i][j] == 'o'? 1 : 0; if(i) psum[i][j] += psum[i - 1][j]; if(j) psum[i][j] += psum[i][j - 1]; if(i && j) psum[i][j] -= psum[i - 1][j - 1]; } } int ans = 0; for(int i = 0; i <= r; i++) { for(int j = 0; j <= c; j++) { re = H - 1 - r + i; ce = W - 1 - c + j; memset(cc, -1, sizeof(cc)); ans = max(ans, calc(i, j, i + rlen - 1, j + clen - 1) + dp(i, j)); } } cout << ans; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> const int MN = 105; int N, M, px, py, A[MN][MN], B[MN][MN], C[MN][MN]; char s[MN][MN]; int f[MN][MN][MN]; int main() { scanf("%d%d", &N, &M); for (int i = 1; i <= N; ++i) { scanf("%s", s[i] + 1); for (int j = 1; j <= M; ++j) { A[i][j] = s[i][j] == 'o'; B[i][j] = B[i][j - 1] + A[i][j]; C[i][j] = C[i - 1][j] + A[i][j]; if (s[i][j] == 'E') px = i, py = j; } } for (int u = 0; u <= px - 1; ++u) { for (int d = 0; d <= N - px; ++d) { for (int l = 0; l <= py - 1; ++l) { for (int r = 0; r <= M - py; ++r) { int tu = std::max(px - u, 1 + d); int td = std::min(px + d, N - u); int tl = std::max(py - l, 1 + r); int tr = std::min(py + r, M - l); if (u && px - u == tu && tl <= tr) f[d][l][r] += B[px - u][tr] - B[px - u][tl - 1]; if (d) f[d][l][r] = std::max(f[d][l][r], f[d - 1][l][r] + (px + d == td && tl <= tr ? B[px + d][tr] - B[px + d][tl - 1] : 0)); if (l) f[d][l][r] = std::max(f[d][l][r], f[d][l - 1][r] + (py - l == tl && tu <= td ? C[td][py - l] - C[tu - 1][py - l] : 0)); if (r) f[d][l][r] = std::max(f[d][l][r], f[d][l][r - 1] + (py + r == tr && tu <= td ? C[td][py + r] - C[tu - 1][py + r] : 0)); } } } } printf("%d\n", f[N - px][py - 1][M - py]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; short line[103][103],list[103][103],f[103][103][103][103],n,m,op,sx,sy; char s[103][103]; inline short min(short a,short b){ if(a<b) return a; return b; } inline short max(short a,short b){ if(a>b) return a; return b; } int main(){ cin>>n>>m; for(short i=1;i<=n;i++){ cin>>(s[i]+1); } for(short i=1;i<=n;i++){ for(short j=1;j<=m;j++){ line[i][j]=line[i][j-1]+(s[i][j]=='o'); list[i][j]=list[i-1][j]+(s[i][j]=='o'); if(s[i][j]=='E'){ sx=i,sy=j; } } } for(short i=sx;i;i--){ for(short j=sy;j;j--){ for(short k=sx;k<=n;k++){ for(short p=sy;p<=m;p++){ if(1<i&&k+1<sx+i){ f[i-1][j][k][p]=max(f[i-1][j][k][p],f[i][j][k][p]+line[i-1][min(p,m-sy+j)]-line[i-1][max(j-1,p-sy)]); op=max(op,f[i-1][j][k][p]); } if(k<n&&sx+k<n+i){ f[i][j][k+1][p]=max(f[i][j][k+1][p],f[i][j][k][p]+line[k+1][min(p,m-sy+j)]-line[k+1][max(j-1,p-sy)]); op=max(op,f[i][j][k+1][p]); } if(1<j&&p+1<sy+j){ f[i][j-1][k][p]=max(f[i][j-1][k][p],f[i][j][k][p]+list[min(k,n-sx+i)][j-1]-list[max(i-1,k-sx)][j-1]); op=max(op,f[i][j-1][k][p]); } if(p<m&&sy+p<m+j){ f[i][j][k][p+1]=max(f[i][j][k][p+1],f[i][j][k][p]+list[min(k,n-sx+i)][p+1]-list[max(i-1,k-sx)][p+1]); op=max(op,f[i][j][k][p+1]); } } } } } cout<<op<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int N=102; short dp[N][N][N][N],sum[N][N]; char a[N][N]; int X,Y,n,m; short getsum(int a,int b,int c,int d) { if(a>c)swap(a,c);if(b>d)swap(b,d); return sum[c][d]-sum[a-1][d]-sum[c][b-1]+sum[a-1][b-1]; } void getmx(short &a,short b){if(a<b)a=b;} int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",a[i]+1); for(int j=1;j<=m;j++) a[i][j]=='E'?X=i,Y=j:(sum[i][j]=(a[i][j]=='o')); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]; short ans=0; for(int a=X;a;a--) for(int b=Y;b;b--) for(int c=X;c<=n;c++) for(int d=Y;d<=m;d++) { ans=max(ans,dp[a][b][c][d]); int A=c-X+1,B=d-Y+1,C=n-X+a,D=m-Y+b; if(a>A)getmx(dp[a-1][b][c][d],dp[a][b][c][d]+getsum(a-1,max(b,B),a-1,min(d,D))); if(b>B)getmx(dp[a][b-1][c][d],dp[a][b][c][d]+getsum(max(a,A),b-1,min(c,C),b-1)); if(c<C)getmx(dp[a][b][c+1][d],dp[a][b][c][d]+getsum(c+1,max(b,B),c+1,min(d,D))); if(d<D)getmx(dp[a][b][c][d+1],dp[a][b][c][d]+getsum(max(a,A),d+1,min(c,C),d+1)); } printf("%d\n",(int)ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define N 105 #define ll long long using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar()); while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return f?x:-x; } int n,m,a[N][N],sx,sy,f[2][N][N][N]; char str[N]; int get(int x1, int x2, int y1, int y2) { return a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]; } inline void _max(int &a, int b) { a = max(a, b); } int main(int argc, char const *argv[]) { n = read(); m = read(); for (int i = 1; i <= n; ++ i) { scanf("%s", str + 1); for (int j = 1; j <= m; ++ j) { if (str[j]=='E') sx = i, sy = j; if (str[j]=='o') ++ a[i][j]; a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } } int ans = 0, now = 0; for (int i = 0; i < sx; ++ i) { now ^= 1; for (int j = 0; j < sy; ++ j) { for (int k = 0; k < n - sx + 1; ++ k) { for (int l = 0; l < m - sy + 1; ++ l) { f[now][j][k][l] = 0; if (i && sx - i > k) _max(f[now][j][k][l], f[now^1][j][k][l] + get(sx-i, sx-i, max(sy-j, l+1), min(sy+l, m-j))); if (j && sy - j > l) _max(f[now][j][k][l], f[now][j-1][k][l] + get(max(sx-i, k+1), min(sx+k, n-i), sy-j, sy-j)); if (k && n-sx-k+1>i) _max(f[now][j][k][l], f[now][j][k-1][l] + get(sx+k, sx+k, max(sy-j, l+1), min(sy+l, m-j))); if (l && m-sy-l+1>j) _max(f[now][j][k][l], f[now][j][k][l-1] + get(max(sx-i, k+1), min(sx+k, n-i), sy+l, sy+l)); _max(ans, f[now][j][k][l]); } } } } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <algorithm> #include <cstdio> #define upd(x, y) (x) = std::max(x, y) char str[105]; int dp[105][105][105][105], row_pre[105][105], col_pre[105][105]; int main() { // freopen("AGC004-E.in", "r", stdin); int n, m, x, y; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", str); for (int j = 0; j < m; j++) { row_pre[i][j] = (j ? row_pre[i][j - 1] : 0) + (str[j] == 'o'); col_pre[j][i] = (i ? col_pre[j][i - 1] : 0) + (str[j] == 'o'); if (str[j] == 'E') { x = i; y = j; } } } auto calc_row = [&] (int row, int l, int r) { int L = std::max(l, r - y), R = std::min(r, l + m - y - 1); return L <= R ? row_pre[row][R] - (L ? row_pre[row][L - 1] : 0) : 0; }; auto calc_col = [&] (int col, int l, int r) { int L = std::max(l, r - x), R = std::min(r, l + n - x - 1); return L <= R ? col_pre[col][R] - (L ? col_pre[col][L - 1] : 0) : 0; }; for (int i = 0; i <= x; i++) { for (int j = n - 1; j >= x; j--) { for (int k = 0; k <= y; k++) { for (int t = m - 1; t >= y; t--) { if (j - i < x) upd(dp[i][j][k][t], dp[i - 1][j][k][t] + calc_row(i - 1, k, t)); if (j - i < n - x - 1) upd(dp[i][j][k][t], dp[i][j + 1][k][t] + calc_row(j + 1, k, t)); if (t - k < y) upd(dp[i][j][k][t], dp[i][j][k - 1][t] + calc_col(k - 1, i, j)); if (t - k < m - y - 1) upd(dp[i][j][k][t], dp[i][j][k][t + 1] + calc_col(t + 1, i, j)); } } } } printf("%d\n", dp[x][x][y][y]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define LL long long #define M 102 using namespace std; int read(){ int nm=0,fh=1; char cw=getchar(); for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh; for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0'); return nm*fh; } int sm[M][M],dp[M][M][M],n,m,X,Y,ans; char ch[M]; inline int sum(int x,int y,int xx,int yy){return sm[xx][yy]+sm[x-1][y-1]-sm[x-1][yy]-sm[xx][y-1];} void upd(int &x,int num){x=max(x,num);} int main(){ n=read(),m=read(); for(int i=1;i<=n;i++){ scanf("%s",ch+1); for(int j=1;j<=m;j++){ sm[i][j]=ch[j]=='o',sm[i][j]+=sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1]; if(ch[j]=='E') X=i,Y=j; } } for(int up=0;up<X;up++){ for(int i=0;i<=n-X;i++){ int U=max(i+1,X-up),D=min(n-up,X+i); if(U>D) continue; for(int j=0;j<Y;j++){ for(int k=0;k<=m-Y;k++){ int L=max(k+1,Y-j),R=min(m-j,k+Y); if(L>R) continue; else upd(ans,dp[i][j][k]); if(Y-j-1>k) upd(dp[i][j+1][k],dp[i][j][k]+sum(U,Y-j-1,D,Y-j-1)); if(Y+k<m-j) upd(dp[i][j][k+1],dp[i][j][k]+sum(U,Y+k+1,D,Y+k+1)); if(X+i<n-up) upd(dp[i+1][j][k],dp[i][j][k]+sum(X+i+1,L,X+i+1,R)); if(X-up-1>i) dp[i][j][k]+=sum(X-up-1,L,X-up-1,R); } } } } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; #define N 116 int n,m,x,y; short a[N][N],b[N][N],f[N][N][N]; void pls(short &x,short y){x=max(x,y);} int read(){ int x=0,f=1;char ch=getchar(); for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-f; for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x*f; } int main(){ n=read();m=read(); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ char ch;scanf(" %c",&ch); if (ch=='E') x=i,y=j; if (ch=='o') a[i][j]=b[i][j]=1; a[i][j]+=a[i][j-1]; b[i][j]+=b[i-1][j]; } for (int i=0;i<=x-1;i++) for (int j=0;j<=n-x;j++) for (int k=0;k<=y-1;k++) for (int l=0;l<=m-y;l++){ int u=max(x-i,1+j),v=min(x+j,n-i); pls(f[j][k][l+1],f[j][k][l]+((y+l+1<=m-k&&u<=v)?(b[v][y+l+1]-b[u-1][y+l+1]):0)); pls(f[j][k+1][l],f[j][k][l]+((y-k-1>=1+l&&u<=v)?(b[v][y-k-1]-b[u-1][y-k-1]):0)); u=max(y-k,1+l);v=min(y+l,m-k); pls(f[j+1][k][l],f[j][k][l]+((x+j+1<=n-i&&u<=v)?(a[x+j+1][v]-a[x+j+1][u-1]):0)); pls(f[j][k][l],f[j][k][l]+((x-i-1>=1+j&&u<=v)?(a[x-i-1][v]-a[x-i-1][u-1]):0)); } printf("%d\n",f[n-x][y-1][m-y]); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 105 using namespace std; short f[maxn][maxn][maxn][maxn]; char s[maxn][maxn]; int a[maxn][maxn]; int X,Y,n,m; void update(short &x,short y) { if(x<y) x=y; } int get(int x1,int y1,int x2,int y2) { return a[x2][y2]-a[x2][y1-1]-a[x1-1][y2]+a[x1-1][y1-1]; } int main() { cin>>n>>m; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]=='o') a[i][j]=1; if(s[i][j]=='E') X=i,Y=j; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; } memset(f,213,sizeof(f)); f[0][0][0][0]=0; short ans=0; for(int l=0;Y-l>=1;l++) { for(int r=0;Y+r<=m;r++) { for(int u=0;X-u>=1;u++) { for(int d=0;X+d<=n;d++) { update(ans,f[l][r][u][d]); int xl=max(X-u,d+1),xr=min(X+d,n-u); int yl=max(Y-l,r+1),yr=min(Y+r,m-l); if(Y-l>=r+2) update(f[l+1][r][u][d],f[l][r][u][d]+get(xl,Y-l-1,xr,Y-l-1)); if(Y+r<=m-l-1) update(f[l][r+1][u][d],f[l][r][u][d]+get(xl,Y+r+1,xr,Y+r+1)); if(X-u>=d+2) update(f[l][r][u+1][d],f[l][r][u][d]+get(X-u-1,yl,X-u-1,yr)); if(X+d<=n-u-1) update(f[l][r][u][d+1],f[l][r][u][d]+get(X+d+1,yl,X+d+1,yr)); } } } } cout<<ans<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) using namespace std; const int N=105; char mp[N][N]; int n,m,ex,ey,ans,dp[N][N][N]; int sr[N][N],sc[N][N]; void mx(int &x,int y){x<y?x=y:0;} int main(){ scanf("%d%d",&n,&m); memset(dp,233,sizeof(dp)); For(i,1,n) scanf("%s",mp[i]+1); For(i,1,n) For(j,1,m){ if (mp[i][j]=='o') sr[i][j]=sc[i][j]=1; else if (mp[i][j]=='E') ex=i,ey=j; sr[i][j]+=sr[i][j-1],sc[i][j]+=sc[i-1][j]; } int L=ey-1,R=m-ey,U=ex-1,D=n-ex; dp[0][0][0]=0; For(l,0,L) For(r,0,R) For(u,0,U) For(d,0,D){ int v=dp[r][u][d]; ans=max(ans,v); int up=max(ex-u,d+1),dn=min(ex+d,n-u),le=max(ey-l,r+1),ri=min(ey+r,m-l); if (up>dn||le>ri) continue; if (u<U-d) mx(dp[r][u+1][d],v+sr[ex-u-1][ri]-sr[ex-u-1][le-1]); if (d<D-u) mx(dp[r][u][d+1],v+sr[ex+d+1][ri]-sr[ex+d+1][le-1]); if (r<R-l) mx(dp[r+1][u][d],v+sc[dn][ey+r+1]-sc[up-1][ey+r+1]); if (l<L-r) mx(dp[r][u][d],v+sc[dn][ey-l-1]-sc[up-1][ey-l-1]); } printf("%d",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> using namespace std; int n , m , x , y , v[105][105][2]; short f[105][105][105][105]; signed main() { scanf("%d %d" , &n , &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { char s = getchar(); while(s != 'o' && s != '.' && s != 'E') s = getchar(); if(s == 'E') x = i , y = j; else if(s == 'o') { v[i][j][0] = v[i - 1][j][0] + 1; v[i][j][1] = v[i][j - 1][1] + 1; } else { v[i][j][0] = v[i - 1][j][0]; v[i][j][1] = v[i][j - 1][1]; } } } int ans = 0; for (int l = 0; l <= y - 1; ++l) { for (int r = 0; r <= m - y; ++r) { for (int u = 0; u <= x - 1; ++u) { for (int d = 0; d <= n - x; ++d) { ans = max(ans , (int)f[l][r][u][d]); if(l + r < y - 1) f[l + 1][r][u][d] = max((int)f[l + 1][r][u][d] , (int)f[l][r][u][d] + v[min(x + d , n - u)][y - l - 1][0] - v[max(x - u - 1 , d)][y - l - 1][0]); if(l + r < m - y) f[l][r + 1][u][d] = max((int)f[l][r + 1][u][d] , (int)f[l][r][u][d] + v[min(x + d , n - u)][y + r + 1][0] - v[max(x - u - 1 , d)][y + r + 1][0]); if(u + d < x - 1) f[l][r][u + 1][d] = max((int)f[l][r][u + 1][d] , (int)f[l][r][u][d] + v[x - u - 1][min(y + r , m - l)][1] - v[x - u - 1][max(y - l - 1 , r)][1]); if(u + d < n - x) f[l][r][u][d + 1] = max((int)f[l][r][u][d + 1] , (int)f[l][r][u][d] + v[x + d + 1][min(y + r , m - l)][1] - v[x + d + 1][max(y - l - 1 , r)][1]); } } } } printf("%d" , ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<stdio.h> #include<algorithm> using namespace std; char in[110][110]; int dp[5100][5100]; int sum[110][110]; inline int rec(int r1,int r2,int c1,int c2){ return sum[r2][c2]-sum[r1][c2]-sum[r2][c1]+sum[r1][c1]; } int main(){ int a,b;scanf("%d%d",&a,&b); for(int i=0;i<a;i++)scanf("%s",in[i]); for(int i=0;i<a;i++)for(int j=0;j<b;j++){ if(in[i][j]=='o')sum[i+1][j+1]=1; } int r,c; for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(in[i][j]=='E'){r=i;c=j;} for(int i=0;i<=a;i++){ for(int j=0;j<b;j++)sum[i][j+1]+=sum[i][j]; } for(int i=0;i<=b;i++){ for(int j=0;j<a;j++)sum[j+1][i]+=sum[j][i]; } int R=r+1; int C=c+1; for(int i=0;i<5100;i++)for(int j=0;j<5100;j++)dp[i][j]=-99999999; dp[0][0]=0; int ret=0; for(int i=0;i<R*(a-r);i++){ for(int j=0;j<C*(b-c);j++){ if(dp[i][j]<0)continue; ret=max(ret,dp[i][j]); int h1=r-i%R; int h2=r+i/R; h1=max(h1,i/R); h2=min(h2,a-1-i%R); int w1=c-j%C; int w2=c+j/C; w1=max(w1,j/C); w2=min(w2,b-1-j%C); // printf("%d %d %d %d: %d %d %d %d %d\n",i/R,i%R,j/C,j%C,dp[i][j],h1,h2,w1,w2); if(i%R<R-1-i/R){ dp[i+1][j]=max(dp[i+1][j],dp[i][j]+rec(r-i%R-1,r-i%R,w1,w2+1)); } if(i/R<a-r-1-i%R){ dp[i+R][j]=max(dp[i+R][j],dp[i][j]+rec(r+i/R+1,r+i/R+2,w1,w2+1)); } if(j%C<C-1-j/C){ dp[i][j+1]=max(dp[i][j+1],dp[i][j]+rec(h1,h2+1,c-j%C-1,c-j%C)); } if(j/C<b-c-1-j%C){ dp[i][j+C]=max(dp[i][j+C],dp[i][j]+rec(h1,h2+1,c+j/C+1,c+j/C+2)); } } } printf("%d\n",ret); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int N=110; char s[N][N]; int n,m,x,y,ans,cnt[N][N],dp0[N][N][N],dp1[N][N][N]; void bemax(int &x,int v){ if (x<v) x=v; if (ans<v) ans=v; } int count(int x1,int y1,int x2,int y2){ return cnt[x2][y2]-cnt[x2][y1-1]-cnt[x1-1][y2]+cnt[x1-1][y1-1]; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%s",s[i]+1); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ cnt[i][j]=cnt[i-1][j]+cnt[i][j-1]-cnt[i-1][j-1]+(s[i][j]=='o'); if (s[i][j]=='E') x=i,y=j; } for (int a=0;a<=x-1;a++){ for (int b=0;b<=n-x;b++) for (int c=0;c<=y-1;c++) for (int d=0;d<=m-y;d++) dp1[b][c][d]=0; for (int b=0;b<=n-x;b++) for (int c=0;c<=y-1;c++) for (int d=0;d<=m-y;d++){ int v=dp0[b][c][d]; int lx=max(x-a,b+1),ly=max(y-c,d+1),rx=min(x+b,n-a),ry=min(y+d,m-c); if (x-a-1>b) bemax(dp1[b][c][d],v+count(x-a-1,ly,x-a-1,ry)); if (x+b+1<n-a+1) bemax(dp0[b+1][c][d],v+count(x+b+1,ly,x+b+1,ry)); if (y-c-1>d) bemax(dp0[b][c+1][d],v+count(lx,y-c-1,rx,y-c-1)); if (y+d+1<m-c+1) bemax(dp0[b][c][d+1],v+count(lx,y+d+1,rx,y+d+1)); } for (int b=0;b<=n-x;b++) for (int c=0;c<=y-1;c++) for (int d=0;d<=m-y;d++) dp0[b][c][d]=dp1[b][c][d]; } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; int H, W, sx, sy; string S[100]; short dp[100][101][100][101]; int sum[102][102]; int rect(int a, int b, int c, int d, int e, int f, int g, int h) { a = max(a, e); b = min(b, f); c = max(c, g); d = min(d, h); return sum[b][d] - sum[b][c] - sum[a][d] + sum[a][c]; } int rec(int top, int bottom, int left, int right) { if(top >= bottom || left >= right) return 0; if(~dp[top][bottom][left][right]) return dp[top][bottom][left][right]; int ret = 0; int b2 = H - (sy - top), t2 = bottom - sy - 1; int r2 = W - (sx - left), l2 = right - sx - 1; if(top > 0) { int add = rect(top - 1, top, left, right, t2, b2, l2, r2); if(add >= 0) ret = max(ret, rec(top - 1, bottom, left, right) + add); } if(left > 0) { int add = rect(top, bottom, left - 1, left, t2, b2, l2, r2); if(add >= 0) ret = max(ret, rec(top, bottom, left - 1, right) + add); } if(bottom < H) { int add = rect(bottom, bottom + 1, left, right, t2, b2, l2, r2); if(add >= 0) ret = max(ret, rec(top, bottom + 1, left, right) + add); } if(right < W) { int add = rect(top, bottom, right, right + 1, t2, b2, l2, r2); if(add >= 0) ret = max(ret, rec(top, bottom, left, right + 1) + add); } return dp[top][bottom][left][right] = ret; } int main() { cin >> H >> W; for(int i = 0; i < H; i++) { cin >> S[i]; for(int j = 0; j < W; j++) { if(S[i][j] == 'E') sy = i, sx = j; } } for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) sum[i + 1][j + 1] = S[i][j] == 'o'; } for(int i = 0; i <= H; i++) { for(int j = 1; j <= W; j++) sum[i][j] += sum[i][j - 1]; } for(int i = 1; i <= H; i++) { for(int j = 0; j <= W; j++) sum[i][j] += sum[i - 1][j]; } memset(dp, -1, sizeof(dp)); cout << rec(sy, sy + 1, sx, sx + 1) << endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstring> #include <cstdio> using namespace std; #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) const int N = 110; const int inf = 0x3f3f3f3f; int n, m, sx, sy, ans, h[N][N], s[N][N], f[N][N][N]; char str[N]; template <class T> inline void in(T &x) { x = 0; int f = 1; char ch = getchar(); for (; ch<'0' || ch>'9';) {if (ch=='-') f=-1; ch = getchar();} for (; ch>='0' && ch<='9';) x = x*10 + ch-'0', ch = getchar(); x *= f; } int main() { int u, d, l, r, cost; in(n), in(m), memset(f, -1, sizeof f); for (int i = 1; i <= n; ++i) { scanf("%s", str + 1); for (int j = 1; j <= m; ++j) { h[i][j] = h[i][j-1]; s[i][j] = s[i-1][j]; if (str[j] == 'E') sx = i, sy = j; if (str[j] == 'o') ++h[i][j], ++s[i][j]; } } f[0][0][0] = 0; for (int t = 0; t <= m - sy; ++t) for (int i = 0; i <= sx - 1; ++i) for(int j = 0; j <= n - sx; ++j) for (int k = 0; k <= sy - 1; ++k) if (~f[i][j][k]) { u = max(j + 1, sx - i), d = min(n - i, sx + j); l = max(t + 1, sy - k), r = min(m - k, sy + t); if (j + 1 <= sx - i - 1) { cost = h[sx - i - 1][r] - h[sx - i - 1][l - 1]; f[i+1][j][k] = max(f[i+1][j][k], f[i][j][k] + cost); } if (sx + j + 1 <= n - i) { cost = h[sx + j + 1][r] - h[sx + j + 1][l - 1]; f[i][j+1][k] = max(f[i][j+1][k], f[i][j][k] + cost); } if (t + 1 <= sy - k - 1) { cost = s[d][sy - k - 1] - s[u - 1][sy - k - 1]; f[i][j][k+1] = max(f[i][j][k+1], f[i][j][k] + cost); } if (sy + t + 1 <= m - k) { cost = s[d][sy + t + 1] - s[u - 1][sy + t + 1]; f[i][j][k] = max(f[i][j][k], f[i][j][k] + cost); } ans = max(f[i][j][k], ans); } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=105; int N,M,X,Y; char mp[MAXN][MAXN]; short dp[MAXN][MAXN][MAXN][MAXN]; short sum[MAXN][MAXN]; short CountRect(int u,int l,int d,int r) { return sum[d][r]-sum[d][l-1]-sum[u-1][r]+sum[u-1][l-1]; } int main() { scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) { scanf("%s",mp[i]+1); for(int j=1;j<=M;j++) if(mp[i][j]=='E') X=i,Y=j; } for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(mp[i][j]=='o'); int ans=0; for(int u=X;u>0;u--) for(int l=Y;l>0;l--) for(int d=X;d<=N;d++) for(int r=Y;r<=M;r++) { ans=max(ans,(int)dp[u][l][d][r]); int U=d-X+1,L=r-Y+1,D=N-(X-u),R=M-(Y-l); if(u>U) dp[u-1][l][d][r]=max((int)dp[u-1][l][d][r],dp[u][l][d][r]+CountRect(u-1,max(L,l),u-1,min(R,r))); if(l>L) dp[u][l-1][d][r]=max((int)dp[u][l-1][d][r],dp[u][l][d][r]+CountRect(max(U,u),l-1,min(D,d),l-1)); if(d<D) dp[u][l][d+1][r]=max((int)dp[u][l][d+1][r],dp[u][l][d][r]+CountRect(d+1,max(L,l),d+1,min(R,r))); if(r<R) dp[u][l][d][r+1]=max((int)dp[u][l][d][r+1],dp[u][l][d][r]+CountRect(max(U,u),r+1,min(D,d),r+1)); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define rep(i, x, y) for(int i = (x); i <= (y); i++) #define ll long long #define N 105 using namespace std; int n, m, sx, sy, h[N][N], s[N][N], f[N][N][N]; char str[N]; inline void upd(int &x, int y) { if(y > x) x = y; } int main() { scanf("%d%d", &n, &m); rep(i, 1, n) { scanf("%s", str+1); rep(j, 1, m) { h[i][j] = h[i][j-1]; s[i][j] = s[i-1][j];//横竖前缀和 if(str[j] == 'E') sx = i, sy = j; if(str[j] == 'o') h[i][j]++, s[i][j]++; } } //rep(i, 1, n) rep(j, 1, m) printf("%d %d: %d %d\n", i, j, h[i][j], s[i][j]); memset(f, -1, sizeof f); f[0][0][0] = 0; int ans = 0; rep(t, 0, m-sy) rep(i, 0, sx-1) rep(j, 0, n-sx) rep(k, 0, sy-1) if(f[i][j][k] != -1) { int u = max(j+1, sx-i), d = min(n-i, sx+j), l = max(t+1, sy-k), r = min(m-k, sy+t), cost; if(j+1 <= sx-i-1) { cost = h[sx-i-1][r]-h[sx-i-1][l-1]; //if(n-i <= d) cost -= h[n-i-1][r]-h[n-i-1][l-1]; upd(f[i+1][j][k], f[i][j][k]+cost); } if(sx+j+1 <= n-i) { cost = h[sx+j+1][r]-h[sx+j+1][l-1]; //if(j+1 >= u) cost -= h[j+1][r]-h[j+1][l-1]; upd(f[i][j+1][k], f[i][j][k]+cost); } if(t+1 <= sy-k-1) { cost = s[d][sy-k-1]-s[u-1][sy-k-1]; //if(m-k <= r) cost -= s[d][m-k]-s[u-1][m-k]; upd(f[i][j][k+1], f[i][j][k]+cost); } if(sy+t+1 <= m-k) { cost = s[d][sy+t+1]-s[u-1][sy+t+1]; //if(t+1 >= l) cost -= s[d][t+1]-s[u-1][t+1]; upd(f[i][j][k], f[i][j][k]+cost); } upd(ans, f[i][j][k]); } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; #define N 105 int n,m,I,J,a[N][N],r[N][N],c[N][N],ans; char s[N]; void upd(int &x,int y){x=max(x,y);} int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;++i){ scanf("%s",s+1); for (int j=1;j<=m;++j){ a[i][j]=s[j]=='o'; if (s[j]=='E'){I=i; J=j;} } } for (int i=1;i<=n;++i) for (int j=1;j<=m;++j){ r[i][j]=r[i][j-1]+a[i][j]; c[i][j]=c[i-1][j]+a[i][j]; } int f[I+1][n-I+1][J+1][m-J+1]; memset(f,0,sizeof f); for (int i1=0;I-i1>=1;++i1) for (int i2=0;I+i2<=n;++i2) for (int j1=0;J-j1>=1;++j1) for (int j2=0;J+j2<=m;++j2){ ans=max(ans,f[i1][i2][j1][j2]); if (I-i1-1>i2) upd(f[i1+1][i2][j1][j2],f[i1][i2][j1][j2]+r[I-i1-1][min(J+j2,m-j1)]-r[I-i1-1][max(J-j1,1+j2)-1]); if (I+i2+1<=n-i1) upd(f[i1][i2+1][j1][j2],f[i1][i2][j1][j2]+r[I+i2+1][min(J+j2,m-j1)]-r[I+i2+1][max(J-j1,1+j2)-1]); if (J-j1-1>j2) upd(f[i1][i2][j1+1][j2],f[i1][i2][j1][j2]+c[min(I+i2,n-i1)][J-j1-1]-c[max(I-i1,1+i2)-1][J-j1-1]); if (J+j2+1<=m-j1) upd(f[i1][i2][j1][j2+1],f[i1][i2][j1][j2]+c[min(I+i2,n-i1)][J+j2+1]-c[max(I-i1,1+i2)-1][J+j2+1]); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> typedef long long ll; ll gi(){ ll x=0,f=1; char ch=getchar(); while(!isdigit(ch))f^=ch=='-',ch=getchar(); while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f?x:-x; } char s[110][110]; short f[101][101][101][101]; short sum1[101][101],sum2[101][101]; short query1(int x,int l,int r){ if(l>r)return 0; return sum1[r][x]-sum1[l-1][x]; } short query2(int x,int l,int r){ if(l>r)return 0; return sum2[x][r]-sum2[x][l-1]; } void cxk(short&a,short b){if(b>a)a=b;} int main(){ #ifdef XZZSB freopen("in.in","r",stdin); freopen("out.out","w",stdout); #endif int n=gi(),m=gi(),ex,ey; for(int i=1;i<=n;++i)scanf("%s",s[i]+1); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){ if(s[i][j]=='E')ex=i,ey=j; sum1[i][j]=sum2[i][j]=s[i][j]=='o'; sum1[i][j]+=sum1[i-1][j]; sum2[i][j]+=sum2[i][j-1]; } memset(f,-63,sizeof f); f[ex][ex][ey][ey]=0; short ans=0; for(int u=ex;u<=n;++u) for(int d=ex;d;--d) for(int r=ey;r<=m;++r) for(int l=ey;l;--l){ int D=1+(u-ex),U=n-(ex-d),L=1+(r-ey),R=m-(ey-l); cxk(ans,f[d][u][l][r]); if(L<l)cxk(f[d][u][l-1][r],f[d][u][l][r]+query1(l-1,std::max(D,d),std::min(U,u))); if(r<R)cxk(f[d][u][l][r+1],f[d][u][l][r]+query1(r+1,std::max(D,d),std::min(U,u))); if(D<d)cxk(f[d-1][u][l][r],f[d][u][l][r]+query2(d-1,std::max(L,l),std::min(R,r))); if(u<U)cxk(f[d][u+1][l][r],f[d][u][l][r]+query2(u+1,std::max(L,l),std::min(R,r))); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
//2017-10-18 //miaomiao // #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; #define For(i, a, b) for(int i = (a); i <= (int)(b); ++i) #define N (100+2) char mp[N][N]; short f[N][N][N][N], sumr[N][N], sumc[N][N]; inline short Calc(short *sum, int l, int r){ if(l > r) return 0; // printf("add = %d\n", sum[r] - sum[l-1]); return sum[r] - sum[l-1]; } inline void Add(short &me, short add){ if(me < add) me = add; } int main(){ int n, m, ex, ey; scanf("%d%d", &n, &m); For(i, 1, n){ scanf("%s", mp[i]+1); For(j, 1, m){ sumr[i][j] = sumr[i][j-1] + (mp[i][j]=='o'); if(mp[i][j] == 'E') ex = i, ey = j; } } For(i, 1, m) For(j, 1, n) sumc[i][j] = sumc[i][j-1] + (mp[j][i]=='o'); // printf("ex = %d ey = %d\n", ex, ey); int ans = 0, A = ex-1, B = n-ex, C = ey-1, D = m-ey; int me, nxt; For(a, 0, A) For(b, 0, B) For(c, 0, C) For(d, 0, D){ ans = max(ans, me = f[a][b][c][d]); // printf("f[%d][%d][%d][%d] = %d\n", a, b, c, d, f[a][b][c][d]); nxt = 0; if(a < A){ // puts("UP"); if(ex-a-1 > b) nxt = Calc(sumr[ex-a-1], max(d+1,ey-c), min(m-c,ey+d)); Add(f[a+1][b][c][d], nxt+me); } nxt = 0; if(b < B){ // puts("DOWN"); if(ex+b+1 < n-a+1) nxt = Calc(sumr[ex+b+1], max(d+1,ey-c), min(m-c,ey+d)); Add(f[a][b+1][c][d], nxt+me); } nxt = 0; if(c < C){ // puts("LEFT"); if(ey-c-1 > d) nxt = Calc(sumc[ey-c-1], max(b+1,ex-a), min(n-a,ex+b)); Add(f[a][b][c+1][d], nxt+me); } nxt = 0; if(d < D){ // puts("RIGHT"); if(ey+d+1 < m-c+1) nxt = Calc(sumc[ey+d+1], max(b+1,ex-a), min(n-a,ex+b)); Add(f[a][b][c][d+1], nxt+me); } // puts("====================="); puts(""); } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> using namespace std; #define fo(i,a,b) for (int i = (a); i < (b); i++) #define N 102 int h, w, v[N][N], ey, ex; short dp[N][N][N][N], ch[N][N], cv[N][N], ans; char gr[N][N]; int main() { scanf("%d %d", &h, &w); fo(i,0,h) { scanf("%s", gr[i]); fo(j,0,w) { if (gr[i][j]=='o') v[i+1][j+1] = 1; if (gr[i][j]=='E') ey = i+1, ex = j+1; } } fo(i,1,h+1) fo(j,1,w+1) { ch[i][j] = ch[i][j-1] + v[i][j]; cv[i][j] = cv[i-1][j] + v[i][j]; } int sx1 = w - ex, sx2 = ex-1, sy1 = h - ey, sy2 = ey-1; fo(x1,0,sx1+1) fo(x2,0,sx2+1) fo(y1,0,sy1+1) fo(y2,0,sy2+1) { ans = max(ans, dp[x1][x2][y1][y2]); int tx1 = min(x1, sx1 - x2), tx2 = min(x2, sx2 - x1), ty1 = min(y1, sy1 - y2), ty2 = min(y2, sy2 - y1); if (x1 + x2 < sx1) dp[x1+1][x2][y1][y2] = max(dp[x1+1][x2][y1][y2], short(dp[x1][x2][y1][y2] + cv[ey + ty1][ex + x1 + 1] - cv[ey-ty2-1][ex + x1 + 1])); if (x1 + x2 < sx2) dp[x1][x2+1][y1][y2] = max(dp[x1][x2+1][y1][y2], short(dp[x1][x2][y1][y2] + cv[ey + ty1][ex - x2 - 1] - cv[ey-ty2-1][ex - x2 - 1])); if (y1 + y2 < sy1) dp[x1][x2][y1+1][y2] = max(dp[x1][x2][y1+1][y2], short(dp[x1][x2][y1][y2] + ch[ey + y1 + 1][ex + tx1] - ch[ey + y1 + 1][ex - tx2 - 1])); if (y1 + y2 < sy2) dp[x1][x2][y1][y2+1] = max(dp[x1][x2][y1][y2+1], short(dp[x1][x2][y1][y2] + ch[ey - y2 - 1][ex + tx1] - ch[ey - y2 - 1][ex - tx2 - 1])); } printf("%d\n", (int)ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define ll long long #define inf(x) (ll)(1e##x) using namespace std; int H,W,s1[110][110],s2[110][110],MX1,MX2,MX3,MX4; char mp[110][110]; struct DP{ int f[10001000]; int& operator () (int a,int b,int c,int d){ return f[a*MX2*MX3*MX4+b*MX3*MX4+c*MX4+d]; } }f; void chmax(int &a,int b){if(b>a) a=b;} int main(){ cin>>H>>W; int x=0,y=0; for(int i=1;i<=H;i++){ cin>>mp[i]+1; for(int j=1;j<=W;j++){ if(mp[i][j]=='E') x=i,y=j; s1[i][j]=s1[i][j-1]+(mp[i][j]=='o'); s2[j][i]=s2[j][i-1]+(mp[i][j]=='o'); } } MX1=x;MX2=H-x+1;MX3=y;MX4=W-y+1; int ans=0; for(int a=0;a<MX1;a++) for(int b=0;b<MX2;b++) for(int c=0;c<MX3;c++) for(int d=0;d<MX4;d++){ int U=min(a,MX1-b-1),D=min(b,MX2-a-1),L=min(c,MX3-d-1),R=min(d,MX4-c-1); if(MX1-b>a+1) chmax(f(a+1,b,c,d),f(a,b,c,d)+s1[x-a-1][y+R]-s1[x-a-1][y-L-1]); if(MX2-a>b+1) chmax(f(a,b+1,c,d),f(a,b,c,d)+s1[x+b+1][y+R]-s1[x+b+1][y-L-1]); if(MX3-d>c+1) chmax(f(a,b,c+1,d),f(a,b,c,d)+s2[y-c-1][x+D]-s2[y-c-1][x-U-1]); if(MX4-c>d+1) chmax(f(a,b,c,d+1),f(a,b,c,d)+s2[y+d+1][x+D]-s2[y+d+1][x-U-1]); chmax(ans,f(a,b,c,d)); } cout<<ans<<'\n'; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int N=101; int n,m,sx,sy; short ans; short dp[N][N][N][N],s1[N][N],s2[N][N]; char s[N]; short Max(short a,short b){ if (a>=b) return a; return b; } int main(){ int i,j,k,l; scanf("%d%d",&n,&m); for (i=1;i<=n;i++){ scanf("%s",s+1); for (j=1;j<=m;j++){ if (s[j]=='E') sx=i,sy=j; s1[i][j]=s2[i][j]=(s[j]=='o'); s1[i][j]+=s1[i][j-1]; s2[i][j]+=s2[i-1][j]; } } for (i=sx;i>=1;i--) for (j=sy;j>=1;j--) for (k=sx;k<=n;k++) for (l=sy;l<=m;l++){ ans=max(ans,dp[i][j][k][l]); if (i>1&&i+sx>k+1) dp[i-1][j][k][l]=Max(dp[i-1][j][k][l],dp[i][j][k][l]+s1[i-1][min(l,m-sy+j)]-s1[i-1][max((int)j-1,l-sy)]); if (k<n&&sx+k<n+i) dp[i][j][k+1][l]=Max(dp[i][j][k+1][l],dp[i][j][k][l]+s1[k+1][min(l,m-sy+j)]-s1[k+1][max(j-1,l-sy)]); if (j>1&&j+sy>l+1) dp[i][j-1][k][l]=Max(dp[i][j-1][k][l],dp[i][j][k][l]+s2[min(k,n-sx+i)][j-1]-s2[max(i-1,k-sx)][j-1]); if (l<m&&sy+l<m+j) dp[i][j][k][l+1]=Max(dp[i][j][k][l+1],dp[i][j][k][l]+s2[min(k,n-sx+i)][l+1]-s2[max(i-1,k-sx)][l+1]); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<set> #include<bitset> #include<map> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std; typedef long long LL; typedef double db; int get(){ char ch; while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-'); if (ch=='-'){ int s=0; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return -s; } int s=ch-'0'; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return s; } const int N = 102; int f[2][N][N][N]; int n,m; int a[N][N],lef[N][N],up[N][N]; int sx,sy; int calc_h(int x,int l,int r){ int L=max(r+1,sy-l),R=min(m-l,sy+r); return L>R?0:(lef[x][R]-lef[x][L-1]); } int calc_w(int y,int u,int d){ int U=max(d+1,sx-u),D=min(n-u,sx+d); return U>D?0:(up[D][y]-up[U-1][y]); } int main(){ n=get();m=get(); fo(i,1,n){ char s[N]; scanf("%s",s+1); fo(j,1,m){ if (s[j]=='o')a[i][j]=1; if (s[j]=='E')sx=i,sy=j; } } fo(i,1,n) fo(j,1,m){ lef[i][j]=lef[i][j-1]+a[i][j]; up[i][j]=up[i-1][j]+a[i][j]; } int ans=0,now=0; f[0][0][0][0]=0; fo(step,1,n+m){ int t=now^1; fo(u,0,sx-1) fo(l,0,sy-1) fo(r,0,m-sy){ ans=max(ans,f[now][u][l][r]); f[t][u][l][r]=0; } fo(u,0,sx-1) fo(l,0,sy-1) fo(r,0,m-sy){ int d=step-1-u-l-r; if (d<0||d>n-sx)continue; if (sx-u-1>d)f[t][u+1][l][r]=max(f[t][u+1][l][r],f[now][u][l][r]+calc_h(sx-u-1,l,r)); if (sx+d+1<=n-u)f[t][u][l][r]=max(f[t][u][l][r],f[now][u][l][r]+calc_h(sx+d+1,l,r)); if (sy-l-1>r)f[t][u][l+1][r]=max(f[t][u][l+1][r],f[now][u][l][r]+calc_w(sy-l-1,u,d)); if (sy+r+1<=m-l)f[t][u][l][r+1]=max(f[t][u][l][r+1],f[now][u][l][r]+calc_w(sy+r+1,u,d)); } now=t; } fo(u,0,sx-1) fo(l,0,sy-1) fo(r,0,m-sy) ans=max(ans,f[now][u][l][r]); printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; #define MAXN 105 template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } char mp[MAXN][MAXN]; int sum[MAXN][MAXN]; int ans[2][MAXN][MAXN][MAXN]; int n, m, x, y; int getsum(int x, int y, int n, int m) { if (x > n || y > m) return 0; else return sum[n][m] - sum[n][y - 1] - sum[x - 1][m] + sum[x - 1][y - 1]; } void update(int &x, int y) { x = max(x, y); } int main() { read(n), read(m); for (int i = 1; i <= n; i++) { scanf("\n%s", mp[i] + 1); for (int j = 1; j <= m; j++) { if (mp[i][j] == 'E') x = i, y = j; if (mp[i][j] == 'o') sum[i][j] = 1; sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } int finalans = 0; for (int i = 1, now = 1, last = 0; i <= n; i++, now ^= 1, last ^= 1) { for (int j = n; j >= i - 1; j--) for (int k = 1; k <= m; k++) for (int l = m; l >= k - 1; l--) { int ti = x - (n - j), tj = x + (i - 1); int tk = y - (m - l), tl = y + (k - 1); update(ans[now][j][k][l], ans[last][j][k][l] + getsum(max(tj, i), max(tk, k), min(tj, j), min(tl, l))); update(ans[now][j][k][l], ans[now][j + 1][k][l] + getsum(max(ti, i), max(tk, k), min(ti, j), min(tl, l))); update(ans[now][j][k][l], ans[now][j][k - 1][l] + getsum(max(ti, i), max(tl, k), min(tj, j), min(tl, l))); update(ans[now][j][k][l], ans[now][j][k][l + 1] + getsum(max(ti, i), max(tk, k), min(tj, j), min(tk, l))); update(finalans, ans[now][j][k][l]); } for (int j = n; j >= x; j--) for (int k = 1; k <= y; k++) for (int l = m; l >= y; l--) ans[last][j][k][l] = 0; } cout << finalans << endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; bool chkmax(int &x,int y){return x<y?x=y,true:false;} bool chkmin(int &x,int y){return x>y?x=y,true:false;} int readint(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,ans; char a[105][105]; int s[105][105],d[105][105][105][105]; int sum(int lx,int rx,int ly,int ry){return (lx<=rx&&ly<=ry?s[rx][ry]-s[rx][ly-1]-s[lx-1][ry]+s[lx-1][ly-1]:0);} int main(){ n=readint(); m=readint(); for(int i=1;i<=n;i++) scanf("%s",a[i]+1); int tx=0,ty=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(a[i][j]=='E') tx=i,ty=j; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+(a[i][j]=='o'); d[1][1][1][1]=0; for(int i=1;i<=tx;i++){ for(int j=1;j<=n-tx+1;j++){ for(int k=1;k<=ty;k++){ for(int l=1;l<=m-ty+1;l++){ int U=tx-i+1,D=tx+j-1,L=ty-k+1,R=ty+l-1; // cout<<"test "<<i<<' '<<j<<' '<<k<<' '<<l<<' '<<d[i][j][k][l]<<' '<<U<<' '<<D<<' '<<L<<' '<<R<<endl; if(U>1) chkmax(d[i+1][j][k][l],d[i][j][k][l]+(D-tx<U-1?sum(U-1,U-1,max(L,R-ty+1),min(R,m-ty+L)):0)); if(D<n) chkmax(d[i][j+1][k][l],d[i][j][k][l]+(n-tx+U>=D+1?sum(D+1,D+1,max(L,R-ty+1),min(R,m-ty+L)):0)); if(L>1) chkmax(d[i][j][k+1][l],d[i][j][k][l]+(R-ty<L-1?sum(max(U,D-tx+1),min(D,n-tx+U),L-1,L-1):0)); if(R<m) chkmax(d[i][j][k][l+1],d[i][j][k][l]+(m-ty+L>=R+1?sum(max(U,D-tx+1),min(D,n-tx+U),R+1,R+1):0)); } } } } cout<<d[tx][n-tx+1][ty][m-ty+1]<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=105; int N,M,X,Y; char mp[MAXN][MAXN]; short dp[MAXN][MAXN][MAXN][MAXN]; short sum[MAXN][MAXN]; short CountRect(int u,int l,int d,int r) { return sum[d][r]-sum[d][l-1]-sum[u-1][r]+sum[u-1][l-1]; } int main() { scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) { scanf("%s",mp[i]+1); for(int j=1;j<=M;j++) if(mp[i][j]=='E') X=i,Y=j; } for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(mp[i][j]=='o'); int ans=0; for(int u=X;u>0;u--) for(int l=Y;l>0;l--) for(int d=X;d<=N;d++) for(int r=Y;r<=M;r++) { ans=max(ans,(int)dp[u][l][d][r]); //printf("dp[(%d,%d),(%d,%d)]=%d ",u,l,d,r,(int)dp[u][l][d][r]); int U=d-X+1,L=r-Y+1,D=N-(X-u),R=M-(Y-l); //printf("Area:(%d,%d),(%d,%d)\n",U,L,D,R); if(u>U) dp[u-1][l][d][r]=max((int)dp[u-1][l][d][r],dp[u][l][d][r]+CountRect(u-1,max(L,l),u-1,min(R,r))); if(l>L) dp[u][l-1][d][r]=max((int)dp[u][l-1][d][r],dp[u][l][d][r]+CountRect(max(U,u),l-1,min(D,d),l-1)); if(d<D) dp[u][l][d+1][r]=max((int)dp[u][l][d+1][r],dp[u][l][d][r]+CountRect(d+1,max(L,l),d+1,min(R,r))); if(r<R) dp[u][l][d][r+1]=max((int)dp[u][l][d][r+1],dp[u][l][d][r]+CountRect(max(U,u),r+1,min(D,d),r+1)); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 105; int f[MAXN][MAXN][MAXN],n,m,mp[MAXN][MAXN],sum[2][MAXN][MAXN],sx,sy,ans; char s[MAXN]; inline void chk(int &x,int a) { if(a>x)x=a; ans=max(ans,x); } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ scanf("%s",s+1); for(int j=1;j<=m;++j){ mp[i][j]=(s[j]=='o'),sum[0][i][j]=sum[0][i][j-1]+mp[i][j],sum[1][i][j]=sum[1][i-1][j]+mp[i][j]; if(s[j]=='E')sx=i,sy=j; } } int ub,db,lb,rb; for(int u=0;sx-u>0;++u) for(int d=0;sx+d<=n;++d){ ub=max(sx-u,d+1); db=min(sx+d,n-u); if(ub>db)continue; for(int l=0;sy-l>0;++l) for(int r=0;sy+r<=m;++r){ lb=max(sy-l,r+1); rb=min(sy+r,m-l); if(lb>rb)continue; if(sy-(l+1)>=1+r)chk(f[d][l+1][r],f[d][l][r]+sum[1][db][sy-(l+1)]-sum[1][ub-1][sy-(l+1)]); if(m-l>=sy+r+1)chk(f[d][l][r+1],f[d][l][r]+sum[1][db][sy+r+1]-sum[1][ub-1][sy+r+1]); if(sx+d+1<=n-u)chk(f[d+1][l][r],f[d][l][r]+sum[0][sx+d+1][rb]-sum[0][sx+d+1][lb-1]); if(sx-(u+1)>=1+d)chk(f[d][l][r],f[d][l][r]+sum[0][sx-(u+1)][rb]-sum[0][sx-(u+1)][lb-1]); } } cout<<ans<<endl; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <algorithm> #define rep(i,j,k) for (i=j;i<=k;i++) #define down(i,j,k) for (i=j;i>=k;i--) using namespace std; const int N=102; short ans,g[N][N][N][N]; int ex,ey,n,m,i,j,l,r,u,d,L,R,row[N][N],col[N][N]; char a[N][N]; void updata(short &x,short y) { if (y>x) x=y; } int main() { // freopen("robot.in","r",stdin); // freopen("robot.out","w",stdout); scanf("%d%d\n",&n,&m); rep(i,1,n) scanf("%s",a[i]+1); rep(i,1,n) rep(j,1,m) { row[i][j]=row[i][j-1]+(a[i][j]=='o'); if (a[i][j]=='E') ex=i,ey=j; } rep(j,1,m) rep(i,1,n) col[i][j]=col[i-1][j]+(a[i][j]=='o'); rep(l,0,m-ey) down(r,m+1,max(l+1,m+2-ey)) rep(u,0,n-ex) down(d,n+1,max(u+1,n+2-ex)) { ans=max(ans,g[u][d][l][r]); i=u+ex+1; if (i<d) { L=max(l+1,r-(m-ey)-1); R=min(r-1,l+ey); if (L<=R) updata(g[u+1][d][l][r],g[u][d][l][r]+row[i][R]-row[i][L-1]); } i=d-(n-ex)-2; if (i>u) { L=max(l+1,r-(m-ey)-1); R=min(r-1,l+ey); if (L<=R) updata(g[u][d-1][l][r],g[u][d][l][r]+row[i][R]-row[i][L-1]); } j=l+ey+1; if (j<r) { L=max(u+1,d-(n-ex)-1); R=min(d-1,u+ex); if (L<=R) updata(g[u][d][l+1][r],g[u][d][l][r]+col[R][j]-col[L-1][j]); } j=r-(m-ey)-2; if (j>l) { L=max(u+1,d-(n-ex)-1); R=min(d-1,u+ex); if (L<=R) updata(g[u][d][l][r-1],g[u][d][l][r]+col[R][j]-col[L-1][j]); } } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define MAX(x,y) x=max(x,y) using namespace std; const int N=105; int n,m,a[N][N],sx,sy,f[2][N][N][N]; char str[N]; int get(int x1,int x2,int y1,int y2) { return a[x2][y2]-a[x2][y1-1]-a[x1-1][y2]+a[x1-1][y1-1]; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { scanf("%s",str+1); for (int j=1;j<=m;j++) if (str[j]=='E') sx=i,sy=j; else if (str[j]=='o') a[i][j]++; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; int ans=0,now=0; for (int i=0;i<sx;i++) { now^=1; for (int j=0;j<sy;j++) for (int k=0;k<n-sx+1;k++) for (int l=0;l<m-sy+1;l++) { f[now][j][k][l]=0; if (i&&sx-i>k) MAX(f[now][j][k][l],f[now^1][j][k][l]+get(sx-i,sx-i,max(sy-j,l+1),min(sy+l,m-j))); if (j&&sy-j>l) MAX(f[now][j][k][l],f[now][j-1][k][l]+get(max(sx-i,k+1),min(sx+k,n-i),sy-j,sy-j)); if (k&&n-sx-k+1>i) MAX(f[now][j][k][l],f[now][j][k-1][l]+get(sx+k,sx+k,max(sy-j,l+1),min(sy+l,m-j))); if (l&&m-sy-l+1>j) MAX(f[now][j][k][l],f[now][j][k][l-1]+get(max(sx-i,k+1),min(sx+k,n-i),sy+l,sy+l)); MAX(ans,f[now][j][k][l]); } } printf("%d",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> #define repu(i,x,y) for (int i=x; i<=y; ++i) using namespace std; int n,m,sum1[110][110],sum2[110][110],a,b; short ans,f[101][101][101][101]; char s[110]; void update(short &x,short y) { if (y>x) x=y; } int main() { scanf("%d%d",&n,&m); repu(i,1,n) { scanf("%s",s+1); repu(j,1,m) { if (s[j]=='E') a=i,b=j; sum1[i][j]=sum1[i][j-1]+(s[j]=='o'); sum2[i][j]=sum2[i-1][j]+(s[j]=='o'); } } repu(i,0,n-a) repu(j,0,a-1) repu(k,0,m-b) repu(l,0,b-1) { update(ans,f[i][j][k][l]); int x=max(b-l,k+1),y=min(b+k,m-l); if (a+i+1+j<=n) update(f[i+1][j][k][l],f[i][j][k][l]+sum1[a+i+1][y]-sum1[a+i+1][x-1]); if (a-j-1-i>0) update(f[i][j+1][k][l],f[i][j][k][l]+sum1[a-j-1][y]-sum1[a-j-1][x-1]); x=max(a-j,i+1),y=min(a+i,n-j); if (b+k+1+l<=m) update(f[i][j][k+1][l],f[i][j][k][l]+sum2[y][b+k+1]-sum2[x-1][b+k+1]); if (b-l-1-k>0) update(f[i][j][k][l+1],f[i][j][k][l]+sum2[y][b-l-1]-sum2[x-1][b-l-1]); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define N 105 using namespace std; int n,m,x,y,f[N][N][N],g[N][N][N],r[N][N],c[N][N]; char st[N]; void maxself(int &x,int y){ x=max(x,y);} int main() { int i,j,k,l; scanf("%d %d\n",&n,&m); for(i=1;i<=n;i++){ scanf("%s",st+1); for(j=1;j<=m;j++){ c[j][i]=c[j][i-1],r[i][j]=r[i][j-1]; if(st[j]=='E') x=i,y=j; if(st[j]=='o') r[i][j]++,c[j][i]++; } } for(i=0;i<x;i++){ memcpy(g,f,sizeof(g)); memset(f,0,sizeof(f)); for(j=0;j<=n-x;j++) for(k=0;k<y;k++) for(l=0;l<=m-y;l++){ maxself(f[j][k][l],g[j][k][l]+(x-i-1>j ? r[x-i-1][min(y+l,m-k)]-r[x-i-1][max(y-k-1,l)] : 0)); maxself(g[j+1][k][l],g[j][k][l]+(x+j+1<=n-i ? r[x+j+1][min(y+l,m-k)]-r[x+j+1][max(y-k-1,l)] : 0)); maxself(g[j][k+1][l],g[j][k][l]+(y-k-1>l ? c[y-k-1][min(x+j,n-i)]-c[y-k-1][max(x-i-1,j)] : 0)); maxself(g[j][k][l+1],g[j][k][l]+(y+l+1<=m-k ? c[y+l+1][min(x+j,n-i)]-c[y+l+1][max(x-i-1,j)] : 0)); } } cout<<g[n-x][y-1][m-y]; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define y1 __zzd001 using namespace std; const int N=105; int n,m,ax,ay,bx,by; char s[N][N]; short g[N][N],dp[N][N][N][N]; short Get(int x1,int y1,int x2,int y2){ return g[x2][y2]-g[x2][y1-1]-g[x1-1][y2]+g[x1-1][y1-1]; } short max(short a,short b){ return a>b?a:b; } short DP(int x1,int y1,int x2,int y2){ // printf("DP %d %d %d %d\n",(int)x1,(int)y1,(int)x2,(int)y2); short &v=dp[x1][y1][x2][y2]; if (~v) return v; if (x1>x2||y1>y2) return v=0; short xL=max(x2-bx+1,x1),xR=min(x1+ax-1,x2); short yL=max(y2-by+1,y1),yR=min(y1+ay-1,y2); return v=max( max(DP(x1,y1,x2,y2-1)+((yR==y2)?Get(xL,y2,xR,y2):0), DP(x1,y1+1,x2,y2)+((yL==y1)?Get(xL,y1,xR,y1):0)), max(DP(x1,y1,x2-1,y2)+((xR==x2)?Get(x2,yL,x2,yR):0), DP(x1+1,y1,x2,y2)+((xL==x1)?Get(x1,yL,x1,yR):0))); } int main(){ memset(g,0,sizeof g); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%s",s[i]+1); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (s[i][j]=='o') g[i][j]++; else if (s[i][j]=='E') ax=i,bx=n-i+1,ay=j,by=m-j+1; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) g[i][j]=g[i][j]+g[i][j-1]+g[i-1][j]-g[i-1][j-1]; memset(dp,-1,sizeof dp); printf("%d",(int)DP(1,1,n,m)); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
//Heaplax #include<bits/stdc++.h> #define N 105 #define LL long long #define LOG(x) cerr<<#x<<" = "<<x<<endl #define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v #define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) char ch;bool fs;void re(int& x) { while(ch=getchar(),ch<33); if(ch=='-')fs=1,x=0;else fs=0,x=ch-48; while(ch=getchar(),ch>33)x=x*10+ch-48; if(fs)x=-x; } using namespace std; int n,m,x,y,f[2][N][N][N],sx[N][N],sy[N][N]; char s[N][N]; void check(int& a,int b) { a=a<b?b:a; } int slx(int x,int a,int b) { if(a>b)return 0; return sy[x][b]-sy[x][a-1]; } int sly(int y,int a,int b) { if(a>b)return 0; return sx[b][y]-sx[a-1][y]; } int main() { re(n),re(m); for(int i=1;i<=n;++i) scanf("%s",s[i]+1); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) sx[i][j]=sx[i-1][j]+(s[i][j]=='o'), sy[i][j]=sy[i][j-1]+(s[i][j]=='o'); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(s[i][j]=='E') x=i,y=j; int ans=0; for(int i=0;i<x;++i) { bool b=i&1; for(int j=0;j<=n-x;++j) for(int k=0;k<y;++k) for(int l=0;l<=m-y;++l) { int& e=f[b][j][k][l]; ans=max(ans,e); check(f[b^1][j][k][l],e+(j<x-i-1?slx(x-i-1,max(y-k,l+1),min(y+l,m-k)):0)); check(f[b][j+1][k][l],e+(x+j+1<=n-i?slx(x+j+1,max(y-k,l+1),min(y+l,m-k)):0)); check(f[b][j][k+1][l],e+(l<y-k-1?sly(y-k-1,max(x-i,j+1),min(x+j,n-i)):0)); check(f[b][j][k][l+1],e+(y+l+1<=m-k?sly(y+l+1,max(x-i,j+1),min(x+j,n-i)):0)); e=0; } } printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<cstdio> #include<algorithm> #define S(a,b,c,d) (s[c][d]-s[c][(b)-1]-s[(a)-1][d]+s[(a)-1][(b)-1]) using namespace std; const int maxn=105; int n,m,x,y,s[maxn][maxn],ans,F[maxn][maxn][maxn][maxn]; char mp[maxn][maxn]; int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%s",mp[i]+1); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+(mp[i][j]=='o'); if(mp[i][j]=='E') x=i,y=j; } for (int i=0;x+i<=n;i++) for (int j=0;x-j;j++) for (int k=0;y+k<=m;k++) for (int t=0;y-t;t++){ if(F[i][j][k][t]>ans) ans=F[i][j][k][t]; if(x+i+j<n) F[i+1][j][k][t]=max(F[i+1][j][k][t],F[i][j][k][t]+S(x+i+1,max(k+1,y-t),x+i+1,min(m-t,y+k))); if(x-i-j>1) F[i][j+1][k][t]=max(F[i][j+1][k][t],F[i][j][k][t]+S(x-j-1,max(k+1,y-t),x-j-1,min(m-t,y+k))); if(y+k+t<m) F[i][j][k+1][t]=max(F[i][j][k+1][t],F[i][j][k][t]+S(max(i+1,x-j),y+k+1,min(n-j,x+i),y+k+1)); if(y-k-t>1) F[i][j][k][t+1]=max(F[i][j][k][t+1],F[i][j][k][t]+S(max(i+1,x-j),y-t-1,min(n-j,x+i),y-t-1)); } printf("%d\n",ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { boolean local = false; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; long lInf = (long) 1e18; double dInf = 1e50; public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); } int[][] mat; int[][] rows; int[][] cols; int[][][][] dp; int h; int w; public int getRow(int i, int l, int r) { if(l > r){ return 0; } if (l == 0) { return rows[i][r]; } return rows[i][r] - rows[i][l - 1]; } public int getCol(int j, int l, int r) { if(l > r){ return 0; } if (l == 0) { return cols[r][j]; } return cols[r][j] - cols[l - 1][j]; } int[] exit = new int[2]; public void solve() { h = io.readInt(); w = io.readInt(); mat = new int[h][w]; rows = new int[h][w]; cols = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char c = io.readChar(); if (c == 'o') { mat[i][j] = 1; } else if (c == 'E') { exit[0] = i; exit[1] = j; } } } for (int i = 0; i < h; i++) { rows[i][0] = mat[i][0]; for (int j = 1; j < w; j++) { rows[i][j] = rows[i][j - 1] + mat[i][j]; } } for (int j = 0; j < w; j++) { cols[0][j] = mat[0][j]; for (int i = 1; i < h; i++) { cols[i][j] = cols[i - 1][j] + mat[i][j]; } } //lrdu dp = new int[exit[1] + 1][w - exit[1] - 1 + 1][exit[0] + 1][h - exit[0] - 1 + 1]; for (int[][][] x : dp) { for (int[][] y : x) { for (int[] z : y) { Arrays.fill(z, -1); } } } int ans = dp(0, 0, 0, 0); io.cache.append(ans); } public int dp(int l, int r, int d, int u) { if (dp[l][r][d][u] == -1) { dp[l][r][d][u] = 0; if (l + r < exit[1]) { dp[l][r][d][u] = Math.max(dp[l][r][d][u], dp(l + 1, r, d, u) + getCol(exit[1] - l - 1, Math.max(u, exit[0] - d), Math.min(h - 1 - d, exit[0] + u))); } if (exit[1] + l + r < w - 1) { dp[l][r][d][u] = Math.max(dp[l][r][d][u], dp(l, r + 1, d, u) + getCol(exit[1] + r + 1, Math.max(u, exit[0] - d), Math.min(h - 1 - d, exit[0] + u))); } if (d + u < exit[0]) { dp[l][r][d][u] = Math.max(dp[l][r][d][u], dp(l, r, d + 1, u) + getRow(exit[0] - d - 1, Math.max(r, exit[1] - l), Math.min(w - 1 - l, exit[1] + r))); } if (exit[0] + d + u < h - 1) { dp[l][r][d][u] = Math.max(dp[l][r][d][u], dp(l, r, d, u + 1) + getRow(exit[0] + u + 1, Math.max(r, exit[1] - l), Math.min(w - 1 - l, exit[1] + r))); } } return dp[l][r][d][u]; } } public static class FastIO { public final StringBuilder cache = new StringBuilder(1 << 13); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() throws IOException { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } }
JAVA
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> using namespace std; const int N=105; int n,m; char map[N][N]; int sx,sy; short sumr[N][N],sumv[N][N],f[N][N][N][N]; inline void upd(short &x,short y){ (x<y)?x=y:0; } inline int max(int x,int y){ return x>y?x:y; } inline int min(int x,int y){ return x<y?x:y; } inline int get(short *a,int l,int r){ return a[r]-a[l-1]; } void readData(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",map[i]+1); } void init(){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(map[i][j]=='E'){ sx=i; sy=j; break; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) sumr[i][j]=sumr[i][j-1]+(map[i][j]=='o'); for(int j=1;j<=m;j++) for(int i=1;i<=n;i++) sumv[j][i]=sumv[j][i-1]+(map[i][j]=='o'); } void dp(){ int ud=sx-1,dd=n-sx,ld=sy-1,rd=m-sy; for(int u=0;u<=ud;u++) for(int d=0;d<=dd;d++) for(int l=0;l<=ld;l++) for(int r=0;r<=rd;r++){ int L=max(sy-l,r+1),R=min(sy+r,m-l); if(L<=R){ upd(f[u+1][d][l][r],f[u][d][l][r]+(((sx-(u+1))>=d+1)?get(sumr[sx-(u+1)],L,R):0)); upd(f[u][d+1][l][r],f[u][d][l][r]+(((sx+(d+1))<=n-u)?get(sumr[sx+(d+1)],L,R):0)); } L=max(sx-u,d+1); R=min(sx+d,n-u); if(L<=R){ upd(f[u][d][l+1][r],f[u][d][l][r]+(((sy-(l+1))>=r+1)?get(sumv[sy-(l+1)],L,R):0)); upd(f[u][d][l][r+1],f[u][d][l][r]+(((sy+(r+1))<=m-l)?get(sumv[sy+(r+1)],L,R):0)); } } printf("%hd\n",f[ud][dd][ld][rd]); } int main(){ readData(); init(); dp(); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(), m = ni(); char[][] map = nm(n,m); int er = -1, ec = -1; for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ if(map[i][j] == 'E'){ er = i; ec = j; } } } int[][] cum = new int[n+1][m+1]; for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ cum[i+1][j+1] = cum[i+1][j] + cum[i][j+1] - cum[i][j] + (map[i][j] == 'o' ? 1 : 0); } } int[][][][] dp = new int[n][][][]; for(int i = 0;i < n;i++){ dp[i] = new int[n-i][][]; for(int j = 0;i+j < n;j++){ dp[i][j] = new int[m][]; for(int k = 0;k < m;k++){ dp[i][j][k] = new int[m-k]; Arrays.fill(dp[i][j][k], -99999999); } } } for(int h = 1;h <= n;h++){ for(int w = 1;w <= m;w++){ for(int i = 0;i+h-1 < n;i++){ for(int j = 0;j+w-1 < m;j++){ if(i <= er && er < i+h && j <= ec && ec < j+w){ int k = i+h-1, l = j+w-1; if(h == 1 && w == 1){ dp[i][k-i][j][l-j] = 0; continue; } // (r1,c1,r2,c2) // ((r2-er),(c2-ec), n-1-(er-r1), m-1-(ec-c1)) if(i+1 <= k){ dp[i][k-i][j][l-j] = Math.max(dp[i][k-i][j][l-j], dp[i+1][k-(i+1)][j][l-j] + get(Math.max(i, k-er), Math.max(j, l-ec), Math.min(i, n-1-(er-(i+1))), Math.min(l, m-1-(ec-j)), cum)); } if(i <= k-1){ dp[i][k-i][j][l-j] = Math.max(dp[i][k-i][j][l-j], dp[i][k-1-i][j][l-j] + get(Math.max(k, k-1-er), Math.max(j, l-ec), Math.min(k, n-1-(er-i)), Math.min(l, m-1-(ec-j)), cum)); } if(j+1 <= l){ dp[i][k-i][j][l-j] = Math.max(dp[i][k-i][j][l-j], dp[i][k-i][j+1][l-(j+1)] + get(Math.max(i, k-er), Math.max(j, l-ec), Math.min(k, n-1-(er-i)), Math.min(j, m-1-(ec-(j+1))), cum)); } if(j <= l-1){ dp[i][k-i][j][l-j] = Math.max(dp[i][k-i][j][l-j], dp[i][k-i][j][l-1-j] + get(Math.max(i, k-er), Math.max(l, (l-1)-ec), Math.min(k, n-1-(er-i)), Math.min(l, m-1-(ec-j)), cum)); } } } } } } out.println(dp[0][n-1][0][m-1]); } static int get(int r1, int c1, int r2, int c2, int[][] cum) { if(r1 > r2 || c1 > c2)return 0; return cum[r2+1][c2+1] - cum[r1][c2+1] - cum[r2+1][c1] + cum[r1][c1]; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
JAVA
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> using namespace std; #define iter(i, n) for (int i = 1; i <= n; ++i) #define forw(i, a, b) for (int i = a; i <= b; ++i) #define NR 110 int n, m, sx[NR][NR], sy[NR][NR], eX, eY; char s[NR][NR]; int f[NR][NR][NR][NR]; inline int count_x(int x, int l, int r) { return l > r ? 0 : sx[x][r] - sx[x][l - 1]; } inline int count_y(int y, int l, int r) { return l > r ? 0 : sy[y][r] - sy[y][l - 1]; } int main() { scanf("%d%d", &n, &m); iter(i, n) { scanf("%s", s[i] + 1); iter(j, m) { sx[i][j] = sx[i][j - 1] + (s[i][j] == 'o'); if (s[i][j] == 'E') eX = i, eY = j; } } iter(i, m) iter(j, n) sy[i][j] = sy[i][j - 1] + (s[j][i] == 'o'); int ans = 0; forw(i, 0, n - eX + 1) { forw(j, 0, min(n - i, eX)) { forw(k, 0, m - eY + 1) { forw(p, 0, min(m - k, eY)) { int &w = f[i][j][k][p]; if (i > 0) w = max(w, f[i-1][j][k][p] + ((eX - i) <= j ? count_x(i, max(k + 1, eY - p), min(m - p, eY + k)) : 0)); if (j > 0) w = max(w, f[i][j-1][k][p] + (((n - j + 1) - eX) <= i ? count_x(n - j + 1, max(k + 1, eY - p), min(m - p, eY + k)) : 0)); if (k > 0) w = max(w, f[i][j][k-1][p] + ((eY - k) <= p ? count_y(k, max(eX - j, i + 1), min(n - j, eX + i)) : 0)); if (p > 0) w = max(w, f[i][j][k][p-1] + (((m - p + 1) - eY) <= k ? count_y(m - p + 1, max(eX - j, i + 1), min(n - j, eX + i)) : 0)); ans = max(ans, w); //printf("f[%d][%d][%d][%d]=%d\n", i,j,k,p,f[i][j][k][p]); } } } } printf("%d\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <algorithm> #define int short const int N = 105; using namespace std; int L[N][N], R[N][N], f[N][N][N][N], ans; int n, m, sx, sy; char s[N]; inline int min(int a, int b) { return a < b ? a : b; } inline int max(int a, int b) { return a > b ? a : b; } signed main() { scanf("%hd%hd", &n, &m); for(int i = 1; i <= n; i ++ ) { scanf("%s",s+1); for(int j = 1; j <= m; j ++ ) { int x = s[j] == 'o'; L[i][j] = L[i][j-1]+x; R[i][j] = R[i-1][j]+x; if(s[j] == 'E') sx = i, sy = j; } } for(int i = sx; i; i -- ) for(int j = sy; j; j -- ) for(int k = sx; k <= n; k ++ ) for(int l = sy; l <= m; l ++ ) { if(1 < i && k+1 < sx+i) ans = max(ans, f[i-1][j][k][l] = max(f[i-1][j][k][l], f[i][j][k][l]+L[i-1][min(l,m-sy+j)]-L[i-1][max(j-1,l-sy)])); if(k < n && sx+k < n+i) ans = max(ans, f[i][j][k+1][l] = max(f[i][j][k+1][l], f[i][j][k][l]+L[k+1][min(l,m-sy+j)]-L[k+1][max(j-1,l-sy)])); if(1 < j && l+1 < sy+j) ans = max(ans, f[i][j-1][k][l] = max(f[i][j-1][k][l], f[i][j][k][l]+R[min(k,n-sx+i)][j-1]-R[max(i-1,k-sx)][j-1])); if(l < m && sy+l < m+j) ans = max(ans, f[i][j][k][l+1] = max(f[i][j][k][l+1], f[i][j][k][l]+R[min(k,n-sx+i)][l+1]-R[max(i-1,k-sx)][l+1])); } printf("%hd\n", ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <algorithm> #include <cstring> #include <cstdio> using namespace std; int f[105][105][105],a[105][105]; char s[105][105]; int Sum(int x1,int y1,int x2,int y2){ return a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]; } int main(){ int h,w,x=0,y=0,ans=0; scanf("%d%d\n",&h,&w); for (int i=1;i<=h;i++){ scanf("%s",s[i]+1); for (int j=1;j<=w;j++){ if (s[i][j]=='E') x=i,y=j; a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+(s[i][j]=='o'); } } memset(f,-0x3f,sizeof f); f[0][0][0]=0; for (int u=0;u<x;u++) for (int d=0;x+d<=h;d++){ int U=max(x-u,d+1),D=min(x+d,h-u); if (U>D) continue; for (int l=0;l<y;l++) for (int r=0;y+r<=w;r++){ int L=max(y-l,r+1),R=min(y+r,w-l); if (L>R || f[d][l][r]<0) continue; ans=max(ans,f[d][l][r]); if (u+d<h-x) f[d+1][l][r]=max(f[d+1][l][r],f[d][l][r]+Sum(x+d+1,L,x+d+1,R)); if (l+r<y-1) f[d][l+1][r]=max(f[d][l+1][r],f[d][l][r]+Sum(U,y-l-1,D,y-l-1)); if (l+r<w-y) f[d][l][r+1]=max(f[d][l][r+1],f[d][l][r]+Sum(U,y+r+1,D,y+r+1)); if (u+d<x-1) f[d][l][r]+=Sum(x-u-1,L,x-u-1,R); } } printf("%d\n",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define x first #define y second using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> ii; typedef pair<ll,ll> pll; int dp[2][101][101][101]; int H,W; string bd[100]; int ei,ej; int cs[2][101][101]; int main() { cin >> H >> W; for (int i=0;i<H;i++) cin >> bd[i]; for (int i=0;i<H;i++) for (int j=0;j<W;j++) { if (bd[i][j]=='E') { ei=i; ej=j; } else if (bd[i][j]=='o') { cs[0][i][j+1]++; cs[1][i+1][j]++; } cs[0][i][j+1]+=cs[0][i][j]; cs[1][i+1][j]+=cs[1][i][j]; } int p=0; for (int i=H;i>=0;i--,p=1-p) for (int j=H-i;j>=0;j--) for (int k=W;k>=0;k--) for (int l=W-k;l>=0;l--) { if (i+j==H || k+l==W) { dp[p][j][k][l]=0; continue; } int &r=dp[p][j][k][l]; int t=0; if (ei+i+1<H-j) { int lb=max(k,ej-l),ub=min(W-l,ej+k+1); if (lb<ub) t+=cs[0][ei+i+1][ub]-cs[0][ei+i+1][lb]; } t+=dp[1-p][j][k][l]; r=t; t=0; if (ei-j-1>=i) { int lb=max(k,ej-l),ub=min(W-l,ej+k+1); if (lb<ub) t+=cs[0][ei-j-1][ub]-cs[0][ei-j-1][lb]; } t+=dp[p][j+1][k][l]; if (t>r) r=t; t=0; if (ej+k+1<W-l) { int lb=max(i,ei-j),ub=min(H-j,ei+i+1); if (lb<ub) t+=cs[1][ub][ej+k+1]-cs[1][lb][ej+k+1]; } t+=dp[p][j][k+1][l]; if (t>r) r=t; t=0; if (ej-l-1>=k) { int lb=max(i,ei-j),ub=min(H-j,ei+i+1); if (lb<ub) t+=cs[1][ub][ej-l-1]-cs[1][lb][ej-l-1]; } t+=dp[p][j][k][l+1]; if (t>r) r=t; } cout << dp[1-p][0][0][0] << endl; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <bits/stdc++.h> #define For(i, j, k) for(int i = j; i <= k; i++) #define Forr(i, j, k) for(int i = j; i >= k; i--) using namespace std; const int N = 105; short n, m, X, Y; short dp[N][N][N][N], cntx[N][N], cnty[N][N]; char S[N][N]; inline void chkmax(short& x, short v){ x = x > v ? x : v; } int main(){ scanf("%hd%hd", &n, &m); For(i, 1, n) scanf("%s", S[i] + 1); For(i, 1, n) For(j, 1, m) cntx[i][j] = cntx[i][j - 1] + (S[i][j] == 'o'); For(i, 1, m) For(j, 1, n) cnty[i][j] = cnty[i][j - 1] + (S[j][i] == 'o'); For(i, 1, n) For(j, 1, m) if(S[i][j] == 'E') X = i, Y = j; short ans = 0; For(a, 0, n) For(b, 0, m) Forr(c, n, a) Forr(d, m, b){ short v = dp[a][b][c][d]; short r = min(b + Y, d), l = max(d - (m + 1 - Y), b), t = min(a + X, c), s = max(c - (n + 1 - X), a); // printf("dp[%d][%d][%d][%d] = %d [%d, %d] [%d, %d]\n", a, b, c, d, v, l, r, s, t); short nx = X + a + 1; if(l <= r && nx <= c) chkmax(dp[a + 1][b][c][d], cntx[nx][r] - cntx[nx][l] + v); nx = c - (n + 1 - X); if(l <= r && nx > a) chkmax(dp[a][b][c - 1][d], cntx[nx][r] - cntx[nx][l] + v); nx = Y + b + 1; if(s <= t && nx <= d) chkmax(dp[a][b + 1][c][d], cnty[nx][t] - cnty[nx][s] + v); nx = d - (m + 1 - Y); if(s <= t && nx > b) chkmax(dp[a][b][c][d - 1], cnty[nx][t] - cnty[nx][s] + v); chkmax(ans, v); } printf("%hd\n", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int H,W,sum[101][101],sx,sy,f[101][101][101],ans; char str[101]; inline int get(int l,int r,int u,int d){return sum[d][r]-sum[d][l-1]-sum[u-1][r]+sum[u-1][l-1];} int main(){ scanf("%d%d",&H,&W); for(int i=1;i<=H;++i){ scanf("%s",str+1); for(int j=1;j<=W;++j){sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(str[j]=='o');if(str[j]=='E')sx=i,sy=j;} } for(int u=0;u<sx;++u)for(int d=0;d<=H-sx;++d){ int U=max(sx-u,d+1),D=min(sx+d,H-u);if(U>D)continue; for(int l=0;l<sy;++l)for(int r=0;r<=W-sy;++r){ int L=max(sy-l,r+1),R=min(sy+r,W-l);if(L>R)continue; ans=max(ans,f[d][l][r]); if(l+r<sy-1)f[d][l+1][r]=max(f[d][l+1][r],f[d][l][r]+get(sy-l-1,sy-l-1,U,D)); if(l+r<W-sy)f[d][l][r+1]=max(f[d][l][r+1],f[d][l][r]+get(sy+r+1,sy+r+1,U,D)); if(u+d<H-sx)f[d+1][l][r]=max(f[d+1][l][r],f[d][l][r]+get(L,R,sx+d+1,sx+d+1)); if(u+d<sx-1)f[d+0][l][r]=max(f[d+0][l][r],f[d][l][r]+get(L,R,sx-u-1,sx-u-1)); } } printf("%d",ans); }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int a[105][105][2], dp[105][105][105][105]; int main () { int n, m, Ex, Ey; scanf ("%d %d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char ch; cin >> ch; if (ch == 'o') a[i][j][0] = a[i][j][1] = 1; if (ch == 'E') Ex = i, Ey = j; } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j][1] += a[i-1][j][1]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j][0] += a[i][j-1][0]; int ans = 0; for (int l = 0; l < Ey; l++) { for (int r = 0; r + Ey <= m; r++) { for (int u = 0; u < Ex; u++) { for (int d = 0; d + Ex <= n; d++) { if (u + d < n - Ex) dp[l][r][u][d+1] = max (dp[l][r][u][d+1], dp[l][r][u][d] + a[Ex+d+1][min(m-l,Ey+r)][0] - a[Ex+d+1][max(r,Ey-l-1)][0]); if (l + r < m - Ey) dp[l][r+1][u][d] = max (dp[l][r+1][u][d], dp[l][r][u][d] + a[min(n-u,Ex+d)][Ey+r+1][1] - a[max(d,Ex-u-1)][Ey+r+1][1]); if (l + r < Ey - 1) dp[l+1][r][u][d] = max (dp[l+1][r][u][d], dp[l][r][u][d] + a[min(n-u,Ex+d)][Ey-l-1][1] - a[max(d,Ex-u-1)][Ey-l-1][1]); if (u + d < Ex - 1) dp[l][r][u+1][d] = max (dp[l][r][u+1][d], dp[l][r][u][d] + a[Ex-u-1][min(m-l,Ey+r)][0] - a[Ex-u-1][max(r,Ey-l-1)][0]); ans = max (ans, dp[l][r][u][d]); } } } } printf ("%d", ans); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; int n,m,x,y,a[110][110],b[110][110],f[10000000],p; char s[110][110]; inline int pos(int i,int j,int k,int l) { return ((i*(n-x+1)+j)*y+k)*(m-y+1)+l+1; } inline void maxx(int &x,int y) { x=max(x,y); } int main() { //freopen(".in","r",stdin); //freopen(".out","w",stdout); int i,j,k,l,u,v,w; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%s",&s[i][0]+1); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { a[i][j]=a[i][j-1]+(s[i][j]=='o'); b[i][j]=b[i-1][j]+(s[i][j]=='o'); if(s[i][j]=='E') x=i,y=j; } for(i=0;i<x;i++) for(j=0;j<=n-x;j++) for(k=0;k<y;k++) for(l=0;l<=m-y;l++) { w=f[pos(i,j,k,l)]; p=max(p,w); if(x-i-1>j) maxx(f[pos(i+1,j,k,l)],w+a[x-i-1][min(y+l,m-k)]-a[x-i-1][max(y-k,l+1)-1]); if(x+j+1<=n-i) maxx(f[pos(i,j+1,k,l)],w+a[x+j+1][min(y+l,m-k)]-a[x+j+1][max(y-k,l+1)-1]); if(y-k-1>l) maxx(f[pos(i,j,k+1,l)],w+b[min(x+j,n-i)][y-k-1]-b[max(x-i,j+1)-1][y-k-1]); if(y+l+1<=m-k) maxx(f[pos(i,j,k,l+1)],w+b[min(x+j,n-i)][y+l+1]-b[max(x-i,j+1)-1][y+l+1]); } printf("%d\n",p); return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> using namespace std; const int N=105; int n,m,Ex,Ey; short f[N][N][N][N],ans,sr[N][N],sc[N][N]; char s[N]; inline short r_sum(int x,int l,int r){return sr[x][r]-sr[x][l-1];} inline short c_sum(int x,int l,int r){return sc[x][r]-sc[x][l-1];} inline void upd(short&x,const short&y){x>y?:x=y;} int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;++i){ cin>>(s+1); for(int j=1;j<=m;++j){ if(s[j]=='E'){Ex=i;Ey=j;} sr[i][j]=sr[i][j-1]; sc[j][i]=sc[j][i-1]; if(s[j]=='o'){sr[i][j]++;sc[j][i]++;} } } for(int i=Ex;i;--i) for(int j=Ex;j<=n;++j) for(int k=Ey;k;--k) for(int l=Ey;l<=m;++l){ ans=max(ans,f[i][j][k][l]); if(i-1>j-Ex)upd(f[i-1][j][k][l],f[i][j][k][l]+r_sum(i-1,max(k,l-Ey+1),min(k+m-Ey,l))); if(j<n-Ex+i)upd(f[i][j+1][k][l],f[i][j][k][l]+r_sum(j+1,max(k,l-Ey+1),min(k+m-Ey,l))); if(k-1>l-Ey)upd(f[i][j][k-1][l],f[i][j][k][l]+c_sum(k-1,max(i,j-Ex+1),min(i+n-Ex,j))); if(l<m-Ey+k)upd(f[i][j][k][l+1],f[i][j][k][l]+c_sum(l+1,max(i,j-Ex+1),min(i+n-Ex,j))); } cout<<ans<<'\n'; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <map> #include <set> #include <stack> #include <queue> #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define per(i,r,l) for(int i=(r);i>=(l);--i) using namespace std; const int maxn=105; typedef short sh; sh a[maxn][maxn],b[maxn][maxn],dp[maxn][maxn][maxn][maxn],ans; int n,m,tx,ty;char ch; short X(int x,int l,int r){return b[x][r]-b[x][l-1];} short Y(int y,int l,int r){return a[r][y]-a[l-1][y];} int main(){ scanf("%d%d",&n,&m); rep(i,1,n) rep(j,1,m){ scanf(" %c",&ch); if(ch=='o') a[i][j]=b[i][j]=1; else if(ch=='E') tx=i,ty=j; } rep(i,1,n) rep(j,1,m) a[i][j]+=a[i-1][j],b[i][j]+=b[i][j-1]; rep(i,0,tx-1) rep(j,0,ty-1) rep(x,0,n-tx) rep(y,0,m-ty){ if(i&&tx-i>x) dp[i][j][x][y]=max(dp[i][j][x][y],(sh)(dp[i-1][j][x][y]+X(tx-i,max(ty-j,y+1),min(ty+y,m-j)))); if(j&&ty-j>y) dp[i][j][x][y]=max(dp[i][j][x][y],(sh)(dp[i][j-1][x][y]+Y(ty-j,max(tx-i,x+1),min(tx+x,n-i)))); if(x&&tx+x<=n-i) dp[i][j][x][y]=max(dp[i][j][x][y],(sh)(dp[i][j][x-1][y]+X(tx+x,max(ty-j,y+1),min(ty+y,m-j)))); if(y&&ty+y<=m-j) dp[i][j][x][y]=max(dp[i][j][x][y],(sh)(dp[i][j][x][y-1]+Y(ty+y,max(tx-i,x+1),min(tx+x,n-i)))); ans=max(ans,dp[i][j][x][y]); } cout<<ans; return 0; }
CPP
p04009 AtCoder Grand Contest 004 - Salvage Robots
We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows: * `.` : This cell is empty. * `o` : This cell contains a robot. * `E` : This cell contains the exit. `E` occurs exactly once in the whole grid. Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times: * Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid. Find the maximum number of robots that can be salvaged. Constraints * 2≤H,W≤100 * a_{ij} is `.`, `o` or `E`. * `E` occurs exactly once in the whole grid. Input The input is given from Standard Input in the following format: H W a_{11}...a_{1W} : a_{H1}...a_{HW} Output Print the maximum number of robots that can be salvaged. Examples Input 3 3 o.o .Eo ooo Output 3 Input 2 2 E. .. Output 0 Input 3 4 o... o... oooE Output 5 Input 5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo Output 12
6
0
#include<bits/stdc++.h> #define ll long long #define ld long double #define db double #define pint pair<int,int> #define mk make_pair #define pb push_back #define eb emplace_back #define ins insert #define fi first #define se second #define Rep(x,y,z) for(int x=y;x<=z;x++) #define Red(x,y,z) for(int x=y;x>=z;x--) using namespace std; const int MAXN=105; char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny; //inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;} inline char gc(){return getchar();} inline int read(){ int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc; for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny; } int h,w,sx,sy,a[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN],F[2][MAXN][MAXN][MAXN];char s[MAXN]; inline void upd(int&x,int y){y>x?x=y:0;} int main(){ // freopen("std.in","r",stdin); // freopen("std.out","w",stdout); h=read(),w=read(); Rep(i,1,h){ scanf("%s",s+1); Rep(j,1,w){ if(s[j]=='E')sx=i,sy=j; a[i][j]=s[j]=='o',b[i][j]=a[i][j]+b[i][j-1],c[i][j]=a[i][j]+c[i-1][j]; } } int now=0; Rep(u,0,sx-1){ Rep(d,0,h-sx)Rep(l,0,sy-1)Rep(r,0,w-sy){ if(u<sx-1)upd(F[now^1][d][l][r],F[now][d][l][r]+(sx-u-1>d?min(sy+r,w-l)>=max(sy-l,r+1)?b[sx-u-1][min(sy+r,w-l)]-b[sx-u-1][max(sy-l,r+1)-1]:0:0)); if(d<h-sx)upd(F[now][d+1][l][r],F[now][d][l][r]+(sx+d+1<h-u+1?min(sy+r,w-l)>=max(sy-l,r+1)?b[sx+d+1][min(sy+r,w-l)]-b[sx+d+1][max(sy-l,r+1)-1]:0:0)); if(l<sy-1)upd(F[now][d][l+1][r],F[now][d][l][r]+(sy-l-1>r?min(sx+d,h-u)>=max(sx-u,d+1)?c[min(sx+d,h-u)][sy-l-1]-c[max(sx-u,d+1)-1][sy-l-1]:0:0)); if(r<w-sy)upd(F[now][d][l][r+1],F[now][d][l][r]+(sy+r+1<w-l+1?min(sx+d,h-u)>=max(sx-u,d+1)?c[min(sx+d,h-u)][sy+r+1]-c[max(sx-u,d+1)-1][sy+r+1]:0:0)); }if(u==sx-1)break; memset(F[now],0,sizeof(now)),now^=1; }cout<<F[now][h-sx][sy-1][w-sy]<<'\n'; return 0; }
CPP