Uestc_ Qiu arrives asteroid UESTC Training for Search algorithm & String<problem b>

Source: Internet
Author: User

B-Miss Qiu arrives asteroid time limit:10000/5000ms (java/others) Memory limit:65536/65535kb (java/others)SubmitStatus

It is true that the man who wins Miss Qiu is better than any boy. One day, Miss Qiu took her sister and came to aNYesMThe asteroid of the row plane. For each landing site, Miss Qiu always like to take her sister to walk: Assuming that the landing site is(r0,C0)   , then they can only select adjacent lattice points and walk around, i.e.(r0–1,C0)   ,(r0+1,C0)       ,(r0,C0–1)   Or(r0,C0+1)       。 After the journey must be strictly in accordance withTurn Right-forward-left-forward-Turn Right...... of the road ahead. But because Miss Qiu is very distressed sister, so the rugged mountains can not reach. You must return the original path when you cannot move forward. Such as.


Q, where is Miss Qiu landing can travel the planet's most land, output the most likely to access the number of lattice points.

Input

First line an integerT,0<T≤ A representation of the number of groups of input data.
For each set of data, the first row has two integersNAndM, representing the number of rows and columns, respectively,0<N,M≤
BelowNRow, per lineMCharacters (0 or 1).
1 represents the reachable place,0 represents the mountain range (inaccessible place).

Output

For each set of data, output an integer followed by a newline, indicating the maximum number of lattice points that may be accessed after a certain point of landing is selected.

Sample Input and output
Sample Input Sample Output
24 31111111111113 3111101111
104

Problem Solving Report:

This is a memory search topic, each lattice corresponds to 4 kinds of morphology, each form has 2 kinds of forms, so there are 8 kinds of states.

F (i,j,k,m), in the lattice (i,j) corresponding to the state of the first m states of K can walk x step

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;Const intMAXN = 1e3 + -;intmaxarrive[maxn][maxn][4][2],r,c;BOOLPASS[MAXN][MAXN];BOOLInmap (intXinty) {   if(X > R | | x <=0|| Y > C | | Y <=0|| !Pass[x][y])return false; return true;}/*you're holding the violin, you're graceful and beautiful, your eyes are running away*/intDfsintXintYintTurnintSt) {   if(maxarrive[x][y][turn][st]! =-1)    returnMaxarrive[x][y][turn][st]; int&ans =Maxarrive[x][y][turn][st]; if(!inmap (x, y))returnAns =0; if(Turn = =0)    {       if(St = =0) ans= DFS (x,y+1,0,1) +1; Elseans= DFS (X-1Y0,0) +1; }   Else if(Turn = =1)    {        if(St = =0) ans= DFS (x+1Y1,1) +1; Elseans= DFS (x,y+1,1,0) +1; }   Else if(Turn = =2)    {        if(St = =0) ans= DFS (x,y-1,2,1) +1; Elseans= DFS (x+1Y2,0) +1; }   Else if(Turn = =3)    {         if(St = =0) ans=1+ DFS (X-1Y3,1); Elseans=1+ DFS (x,y-1,3,0); }   returnans;}intMainintargcChar*argv[]) {  intCase ; scanf ("%d",&Case );  while(case--) {memset (pass,true,sizeof(pass)); memset (Maxarrive,-1,sizeof(maxarrive)); scanf ("%d%d",&r,&c); Charbuffer[ the];  for(inti =1; I <= R; ++i) {scanf ("%s", buffer);  for(intj =0; J < C; ++j)if(Buffer[j] = ='0') Pass[i][j+1] =false; }      intAns =0;  for(inti =1; I <= R; ++i) for(intj =1; J <= C; ++j) {if(Pass[i][j]) {intNewans =1; Newans+ = DFS (i-1J0,0);// upNewans + = DFS (i+1J2,0);// DownNewans + = DFS (i,j-1,3,0);// LeftNewans + = DFS (i,j+1,1,0);// RightAns =Max (Ans,newans); }} printf ("%d\n", ans); }  return 0;}

Uestc_ Qiu arrives asteroid UESTC Training for Search algorithm & String<problem b>

Related Article

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.