【第二次周赛补题】

7-1 输出全排列

思路:基本的搜索:

代码:

#include<bits/stdc++.h>
using namespace std;
bool book[100100];
int cnt=0;
int a[1000010];
int n;
void DFS(int temp){
	if(temp == n){
		for(int i=1;i<=n;i++)cout<<a[i];
		cout<<endl;
		return ;
	}
	for(int i=1;i<=n;i++){
		if(book[i]==0){
			book[i]=1;
			cnt++;
			a[cnt]=i;
			DFS(temp+1);
			cnt--;
			book[i]=0;
		}
	}
}
int main(){
	cin>>n;
	DFS(0);
	return 0;
}

7-2 山

思路:基本的搜索。

代码:

#include<bits/stdc++.h>
using namespace std;
struct EVA{
	int x,y;
};
bool book[2021][2022];
int mapp[2022][2021];
int n,m;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
void BFS(int qix,int qiy){
	EVA qi;
	qi.x=qix;
	qi.y=qiy;
	queue<EVA>A;
	A.push(qi);
	while(!A.empty()){
		EVA wei=A.front();A.pop();
		for(int i=0;i<4;i++){
			EVA v;
			v.x = wei.x+fx[i];
			v.y = wei.y+fy[i];
			if(v.x<1 || v.x>n || v.y<1 || v.y>m)continue;
			if(book[v.x][v.y] || !mapp[v.x][v.y])continue;
			book[v.x][v.y]=1;
			A.push(v);
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mapp[i][j];
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(book[i][j] || !mapp[i][j])continue;
			ans++;
			BFS(i,j);
		}
	}
	cout<<ans;
	return 0;
}

7-3 跳跃

思路:BFS无本质的变化。

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e6+5;
int n,m[maxn],start;
bool vis[maxn];
queue <int> q;

int main()
{
	cin>>n;
	for(int i=0; i<n; i++) cin>>m[i];
	cin>>start;
	int flag=0;
	q.push(start);
	vis[start]=true;
	while(!q.empty()){
		int t=q.front();
		if(m[t]==0){
			flag=1;
			break;
		}
		q.pop();
		if(t+m[t]<n && !vis[t+m[t]]){
			q.push(t+m[t]);
			vis[t+m[t]]=true;
		}
		if(t-m[t]>=0 && !vis[t-m[t]]){
			q.push(t-m[t]);
			vis[t-m[t]]=true;
		} 
	}
	if(flag) cout<<"True";
	else cout<<"False";
	return 0;
}

7-4 最长光路

最牛的一道题,标记总共有x,y,方向三个方面。

其中可以一格一格地走,也可以用while压缩路径。

代码:


#include<bits/stdc++.h>

using namespace std;
int n, m;
int x;
int y;
int w0 = 0;
int map1[505][505];
//0 . 1 c 2 / 3 \ ; 
int ans[4];
int ans1[4];
bool again[505][505][4];

//0 u 1 r 2 d 3 l
string temp;
void dfs(int x, int y, int w) {
	//cout << w0 << " " << x << " " << y << endl;
	if (x<1 || x>n) {
		return;
	}
	if (y<1 || y>m) {
		return;
	}
	if (again[x][y][w] == 1) {
		ans1[w0]=1;
		return;
	}
	if (map1[x][y] == 1) {
		return;
	}
	again[x][y][w] = 1;
	ans[w0]++;
	if (map1[x][y] == 2) {
		if (w == 0)dfs(x ,y+1, 1);
		if (w == 1)dfs(x - 1, y, 0);
		if (w == 2)dfs(x, y - 1, 3);
		if (w == 3)dfs(x + 1, y, 2);
	}
	else if (map1[x][y] == 3) {
		if (w == 0)dfs(x, y - 1, 3);
		if (w == 1)dfs(x + 1, y, 2);
		if (w == 2)dfs(x, y + 1, 1);
		if (w == 3)dfs(x - 1, y, 0);
	}
	else {
		if (w == 0)dfs(x - 1, y, 0);
		if (w == 1)dfs(x, y + 1, 1);
		if (w == 2)dfs(x + 1, y, 2);
		if (w == 3)dfs(x, y - 1, 3);
	}
	again[x][y][w] = 0;
}
int main() {
	//freopen("voyager7b.in.txt", "r", stdin);
	char way[4] = { 'U','R','D','L' };
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> temp;
		for (int j = 1; j <= m; j++) {
			
			if (temp[j-1] == '.') {
				map1[i][j] = 0;
			}
			if (temp[j-1] == 'C') {
				map1[i][j] = 1;
			}
			if (temp[j-1] == '/') {
				map1[i][j] = 2;
			}
			if (temp[j-1] == '\\') {
				map1[i][j] = 3;
			}
		}
	}
	
	cin >> x >> y;
	for (; w0 < 4; w0++) {
		dfs(x, y, w0);
		if(ans1[w0])break;
	}
	for (int i = 0; i < 4; i++) {
		if (ans1[i] ) {
			cout << way[i] << endl;
			cout << "COOL";
			return 0;
		}
	}
	int maxn = 0, maxi = 0;
	for (int i = 0; i < 4; i++) {
		if (ans[i] > maxn) {
			maxn = ans[i];
			maxi = i;
		}
	}
	cout << way[maxi] << endl;
	cout << maxn;
	return 0;
}


7-5 回文数文回

abcdefghi
每个abcde有且只对应一个回文
其实只用判断最后一位是不是就可以了

代码:

#include<bits/stdc++.h>
using namespace std;
string str;
char a[5],zh,b[5];
int cnt=0,n;
string ce="123456789";
string To_string(int n){
	int cnt=9;
	while(n){
		cnt--;
		//cout<<"cnt = "<<cnt<<endl;
		ce[cnt]=(char)(n%10+'0');
		n/=10;
	}
	return ce;
}
int main(){
	//freopen("data.txt","r",stdin);
	//freopen("prob.out","w",stdout);
	cin>>n;
	str=To_string(n);
	//cout<<str<<endl;
	for(int i=1;i<=9;i++){
		if(i<5)a[++cnt]=str[i-1];
		else if(i==5)cnt=0,zh=str[i-1];
		else b[++cnt]=str[i-1];
	}
	int ans=0;
	for(int i=1;i<=4;i++){
		if(b[i]==a[5-i])continue;
		if(b[i]<a[5-i]){
			ans=-1;
		}
		break;
	}
	int ANS=(a[1]-'1')*10000+(a[2]-'0')*1000+(a[3]-'0')*100+(a[4]-'0')*10+(zh-'0')+1;
	ANS+=ans;
	cout<<ANS;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值