Codeforces round #224 (Div. 2) d brute force search and memory

Source: Internet
Author: User

I have been reading the question for six months. The English is too scum. The question is that two pieces are placed on the chessboard as the starting point, but the starting point cannot be #. Then I start to follow the instructions shown in the figure, <left> right ^ up/down V, you can only follow the chart instructions when you go. If it is # in front, you can go in, but you can no longer go after you walk in, on the way, the two pawns cannot touch each other, but it is okay to eventually go to the same #, and if it can go infinitely, output-1, for example, <this way, the left and right sides can walk infinitely, and then you can ask the sum of the two chess pieces in the maximum number of steps


At first, it was trapped by output-1, because except for.> <in this way, a circle can also be formed. This is not a good decision, and it is not dare to write DFS. Because the figure is 2000*2000, it is a little big, and the reverse DFS is not expected, there is no way to perform a memory-based search. If we set dis [I] [J] to represent the farthest step from (I, j) as the starting point, we feel that time should pass, then enumerate each vertex as the start point for deep search. Here we can determine whether it is-1. Because the figure is 2000*2000, you can take up to 4000000 steps, if the two pawns follow the same steps one after the other, they will not exceed 8000000 at most. Therefore, you can set a maximum value of maxn = 8000000. This value is returned once the marked point is passed, you can determine whether it is-1,

After finding the maximum number of steps for each vertex as the starting point, start searching. If the two vertices have the same maximum number of steps, and they do not touch each other during the process, in this way, the maximum number of steps is ans + ans. If you cannot find the number of steps, placing two pawns in the previous step is definitely the best, that is, ANS + ANS-1, deep Search is a bit tricky,


const int MAXN = 8000000 + 55;char aa[2000 + 55][2000 + 55];int mp[2000 + 55][2000 + 55];int xx[5] = {-1,1,0,0};int yy[5] = {0,0,-1,1};int dis[2000 + 55][2000 + 55];bool vis[2000 + 55][2000 + 55];int bb[2000 + 55][2000 + 55];int n,m;int ans;void init() {memset(aa,0,sizeof(aa));memset(mp,0,sizeof(mp));memset(dis,-1,sizeof(dis));memset(vis,0,sizeof(vis));memset(bb,-1,sizeof(bb));}bool input() {while(scanf("%d %d",&n,&m) == 2) {for(int i=0;i<n;i++) {scanf("%s",aa[i]);for(int j=0;j<m;j++) {if(aa[i][j] == '#')mp[i][j] = -1;if(aa[i][j] == '^')mp[i][j] = 0;if(aa[i][j] == 'v')mp[i][j] = 1;if(aa[i][j] == '<')mp[i][j] = 2;if(aa[i][j] == '>')mp[i][j] = 3;}}return false;}return true;}bool isok(int x,int y) {if(x <0 || x >=n || y < 0 || y >= m)return true;return false;}int dfs1(int x,int y) {if(isok(x,y))return 0;if(vis[x][y])return MAXN;if(dis[x][y] != -1) return dis[x][y];vis[x][y] = 1;if(mp[x][y] == -1) {vis[x][y] = 0;dis[x][y] = 0;return 0;}else {int tmp = dfs1(x + xx[mp[x][y]],y + yy[mp[x][y]]) + 1;vis[x][y] = 0;dis[x][y] = tmp;return tmp;}}int dfs2(int x,int y,int cnt) {if(bb[x][y] != -1) {if(bb[x][y] != cnt || mp[x][y] == -1)return 1;else return 0;}if(mp[x][y] == -1) {bb[x][y] = cnt;return 1;}else {bb[x][y] = cnt;return dfs2(x + xx[mp[x][y]],y + yy[mp[x][y]],cnt + 1);}}void cal() {ans = 0;int mark;for(int i=0;i<n;i++) {for(int j=0;j<m;j++) {if(mp[i][j] == -1)continue;if(dis[i][j] != -1)continue;int tmp = dfs1(i,j);if(tmp >= MAXN){ans = MAXN;return;}ans = max(ans,tmp);}}if(ans == 0)return ;mark = 0;for(int i=0;i<n;i++) {for(int j=0;j<m;j++) {if(dis[i][j] == ans) {if(dfs2(i,j,1))mark++;if(mark > 1){ans *= 2;return ;}}}}ans += (ans - 1);}void output() {if(ans >= MAXN)puts("-1");else cout<<ans<<endl;}int main () {while(true) {init();if(input())return 0;cal();output();}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.