Informatics Olympiad One Pass 1212: LETTERS search and traceback (rake)

1212:LETTERS

Time limit: 1000 ms Memory limit: 65536 KB
Number of commits: 11866 Passes: 5252
[Title description]
Give a roe × col uppercase letter matrix, the starting position is the upper left corner, you can move up, down, left and right , And can't move to the letters that have passed. Ask a maximum of a few letters.

[Input] On the
first line, enter the number R of the letter matrix and the number S of columns, 1≤R, S≤20.

Then output the letter matrix of row R and column S.

[Output] The
maximum number of different letters that can be traversed.

[Sample input]
3 6
HFDFFB
AJHGDH
DGAGEH
[Sample output]
6

Explanation: The first piece of code is in accordance with the title AC, and the second piece of code is the code with the maximum output path added. (The second piece of code is a reminder from my child, I need to deepen this question, add one more question, and record the output path)

#include<bits/stdc++.h>
using namespace std;
int a[25][25],ans,r,s;
bool v[25][25],e[27]; 
int f[4][2]={{0,1},{1,0},{-1,0},{0,-1}}; // 四个方向 
bool is(int x,int y){
	return x>=0 && y>=0 && x<r && y<s && e[a[x][y]]==0 && v[x][y]==0;  	
}
void dfs(int x,int y,int sum){
	ans = max(ans,sum);
	for(int i=0;i<4;i++){
		int xx = x + f[i][0];
		int yy = y + f[i][1];
		if(is(xx,yy)){
			v[xx][yy] = true;
			e[a[xx][yy]] = true;
//			printf("xx:%d,yy:%d\n",xx,yy);
			dfs(xx,yy,sum+1);		
			v[xx][yy] = false;
			e[a[xx][yy]] = false;				
		}
	}
	return;
}
int main(){
 
	cin>>r>>s;
	for(int i=0;i<r;i++){
		for(int j=0;j<s;j++){
			char tmp;
			cin>>tmp;
			a[i][j] = tmp - 'A';
		}
	}
//	for(int i=0;i<r;i++){
//		for(int j=0;j<s;j++){
//			printf("%d ",a[i][j]);
//		}
//		cout<<endl;
//	}
	
	e[a[0][0]] = true;
	v[0][0] = true;
	dfs(0,0,1);
	cout<<ans;
	return 0;
}
/*
3 6
HFDFFB
AJHGDH
DGAGEH
*/

Use v to store the past address and output it.

#include<bits/stdc++.h>
using namespace std;
int a[25][25],ans,r,s;
int v[25][25],e[27],b[25][25]; 
int f[4][2]={{0,1},{1,0},{-1,0},{0,-1}}; // 四个方向 
bool is(int x,int y){
	return x>=0 && y>=0 && x<r && y<s && e[a[x][y]]==0 && v[x][y]==0;  	
}
void dfs(int x,int y,int sum){
//	ans = max(ans,sum);
	if(ans<sum){
		ans = sum;
		for(int i=0;i<r;i++){
			for(int j=0;j<s;j++){
				b[i][j] = v[i][j];
			}
		}
	}
	for(int i=0;i<4;i++){
		int xx = x + f[i][0];
		int yy = y + f[i][1];
		if(is(xx,yy)){
			v[xx][yy] = sum+1;
			e[a[xx][yy]] = true;
//			printf("xx:%d,yy:%d\n",xx,yy);
			dfs(xx,yy,sum+1);		
			v[xx][yy] = false;
			e[a[xx][yy]] = false;				
		}
	}
	return;
}
int main(){
	freopen("cpp.in","r",stdin);
	freopen("cpp.out","w",stdout);
	cin>>r>>s;
	for(int i=0;i<r;i++){
		for(int j=0;j<s;j++){
			char tmp;
			cin>>tmp;
			a[i][j] = tmp - 'A';
		}
	}
//	for(int i=0;i<r;i++){
//		for(int j=0;j<s;j++){
//			printf("%d ",a[i][j]);
//		}
//		cout<<endl;
//	}
	
	e[a[0][0]] = true;
	v[0][0] = true;
	dfs(0,0,1);
	cout<<ans<<endl;
	for(int i=0;i<r;i++){
		for(int j=0;j<s;j++){
			printf("%d ",b[i][j]);
		}
		cout<<endl;
	}
	return 0;
}
/*
3 6
HFDFFB
AJHGDH
DGAGEH
*/
Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105558252