讲课代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;
const int N = 20;
char m[N][N];
bool vis[N]; 
int cnt = 0;
int n, sum;

void dfs(int x, int num)
{
	if(num == sum){
		cnt++;
		return;
	}
	if(x > n) return;
	
	//选择这一列
	for(int i = 1; i <= n; i++)
	{
	
	//vis[i]代表之前有没有选择这一列, 没有选择的话才能选择
		if(m[x][i] == '#' && !vis[i])
		{
			vis[i] = true;
			dfs(x + 1, num + 1);
			vis[i] = false;
		}
		
	}
	//x这一行不选, 直接进入x+1行
	dfs(x + 1, num);
	return;
}

int main()
{
	while(cin >> n >> sum)
	{
		if(n == -1 && sum == -1)
			break;
		
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				cin >> m[i][j];
		
	
		cnt = 0;
		dfs(1, 0);
		cout << cnt << endl;
	}
	
	return 0;
 } 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>

using namespace std;
char map[35][35][35];
int s, n, k,m;
int sx, sy, sz;
int ex, ey, ez;
bool vis[35][35][35];
int dd[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct Node{
	int x, y , z, step;
};
queue<Node> q; 
int bfs(){
	while(!q.empty()){
		q.pop();
	}
	Node a, b;
	a.x = sx;
	a.y = sy;
	a.z = sz;
	a.step = 1;
	vis[sx][sy][sz] = true;
	q.push(a);
	while(!q.empty()){
		a = q.front();
		q.pop();
		
		int x = a.x;
		int y = a.y;
		int z = a.z;
		for(int i = 0; i < 6; i++){
			int dx = x + dd[i][0];
			int dy = y + dd[i][1];
			int dz = z + dd[i][2];
			if(dx == ex && dy == ey & dz == ez){
				return a.step;
			}
			if(dx > 0 && dx <= k && dy > 0 && dy <= n && dz > 0 && dz <= m && map[dx][dy][dz] != '#' && !vis[dx][dy][dz]){
				vis[dx][dy][dz] = true;
				b.x = dx;
				b.y = dy;
				b.z = dz;
				b.step = a.step + 1;
				q.push(b);
			}
		}
	}
	return 0;
	
}
int main(){	
	while(~scanf("%d%d%d", &k, &n, &m)){
		if(n == 0 && k == 0 && m == 0) break;
		memset(vis, false, sizeof(vis));
		memset(map, '\0', sizeof(map));
		for(int i = 1; i <= k; i++){
			for(int j = 1; j <= n; j++){
				for(int l = 1; l <= m; l++){
					cin >> map[i][j][l];
					if(map[i][j][l] == 'S'){
						sx = i;
						sy = j;
						sz = l;
					}
					if(map[i][j][l] == 'E'){
						ex = i;
						ey = j;
						ez = l;
					}
				}
			}
		}
		int ans = bfs();
		if(ans)
		cout << "Escaped in " << ans << " minute(s)." << endl;
		else cout << "Trapped!" << endl;
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>

using namespace std;
struct Node{
	int x;
	int step;
};
bool vis[100010];
int m, n, now, next;
int bfs(int n, int m){
	if(n == m) return 0;
	memset(vis,0,sizeof(vis));
	queue<Node> q;
	Node a, b;
	a.x = n;
	a.step = 0;
	q.push(a);
	vis[n] = true;
	while(!q.empty()){
		a = q.front();
		q.pop();
		for(int i = 0; i < 3; i++){
			b = a;
			if(i == 0) b.x--;
			if(i == 1) b.x++;
			if(i == 2) b.x <<= 1;
			if(b.x >= 0 && b.x <= 100010 && !vis[b.x]){
				b.step++;
				if(b.x == m) return b.step;
				vis[b.x] = true;
				q.push(b);
			}
		}
	}
}
int main(){	
	
	while(~scanf("%d%d", &n, &m)){
		printf("%d\n", bfs(n,m));
	}

	return 0;
}


2.0
#include <iostream>
#include <cstring>
#include <queue> 
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
queue<PII> q;
int n, k;
bool vis[N];

int bfs(int s, int e)
{
	q.push({s, 0});
	if(s == e) return 0;
	while(q.size())
	{
		PII t = q.front();
		q.pop();
		
		//x * 2
		int g = t.first * 2;
		if(g == e)
			return t.second + 1;
		if(g < 100000 && !vis[g])
		{
			vis[g] = true;
			q.push({g, t.second + 1});	
		}
			
			
		//x + 1
		g = t.first + 1;
		if(g == e)
			return t.second + 1;
		if(g < 100000 && !vis[g])
		{
			vis[g] = true;
			q.push({g, t.second + 1});	
		}
		
		//x - 1
		g = t.first - 1;
		if(g == e)
			return t.second + 1;
		if(g > 0 && !vis[g])
		{
			vis[g] = true;
			q.push({g, t.second + 1});	
		}
	}	
}
 
int main()
{
	cin >> n >> k;
	cout << bfs(n, k) << endl;	
	
	return 0;
 } 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>

using namespace std;
int n;
int flag;
void dfs(long long x, int t){
	if(flag)
	return;
	if(t >= 19)
	return;
	if(x % n == 0){
		printf("%lld\n", x);
		flag = 1;
		return;
	}
	else{
		dfs(x*10, t+1);
		dfs(x*10+1, t+1);
	}
	
	
	
}

int main(){	
	
	while(~scanf("%d", &n) && n){
		flag = 0;
		dfs(1,0);
	}

	return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>
#include<ctime>

using namespace std;
int map[100][100];
int vis[100][100];
int dis[100][100];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
struct node
{
    int x, y;
}pre[100][100];
queue<node> q;
int m, n;
void bfs()
{
    while(!q.empty())
        q.pop();
    memset(vis, 0, sizeof(vis));
    dis[0][0] = 0;
    vis[0][0] = 1;
    node temp;
    temp.x = temp.y = 0;
    q.push(temp);
    while(q.size())
    {
        node temp = q.front();
        q.pop();
        int x = temp.x;
        int y = temp.y;
        for(int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx >= 0 && nx <= n && ny >= 0 && ny < m && !vis[nx][ny] && map[nx][ny] == 0)
            {
                vis[nx][ny] = 1;
                temp.x = nx;
                temp.y = ny;
                q.push(temp);
                dis[nx][ny] = dis[x][y] +1;
                temp.x = x; temp.y = y;
				pre[nx][ny] = temp;
                if (nx == n && ny == m)
                    return;

            }
        }
    }
}

void print(int x, int y) //递归打印
{
	if (x == 0 && y == 0)return;
	print(pre[x][y].x, pre[x][y].y);
	printf("(%d, %d)\n", pre[x][y].x, pre[x][y].y);
}
int main()
{
    n = 5;
    m = 5;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
       cin >> map[i][j];
    memset(dis, 0, sizeof(dis));
    bfs();
    print(n-1,m-1);
    printf("(%d, %d)\n", 4,4);
}

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int N = 10;
int m[N][N];
struct node{
	int x, y;
};
node pre[100][100];
queue<node> q;
bool vis[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

bool check(int x, int y)
{
	if(x >= 1 && x <= 5 && y >= 1 && y <= 5 && !vis[x][y] && m[x][y] == 0)
		return true;
	return false;
}

void bfs()
{
	q.push({1, 1});
	while(q.size())
	{
		node t = q.front();
		q.pop();
		int x = t.x;
		int y = t.y;
		for(int i = 0; i < 4; i++)
		{
			int fx = x + dx[i];
			int fy = y + dy[i];
			if(check(fx, fy))
			{
				vis[fx][fy] = true;
				q.push({fx, fy});
				pre[fx][fy] = {x, y};	
				if(fx == 5 && fy == 5)
					return;
			}
			
		}
	}
}

void print(int x, int y)
{
	if(x == 1 && y == 1)
	{
		cout << "(" << x - 1 << ", " << y - 1 << ")" <<endl;
		return;
	}
	
	print(pre[x][y].x, pre[x][y].y);	
	cout << "(" << x - 1 << ", " << y - 1 << ")" <<endl;
}

int main()
{
	for(int i = 1; i <= 5; i++)
		for(int j = 1; j <= 5; j++)
			cin >> m[i][j];
	
	bfs();
	print(5, 5);
	
	return 0;
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
int is_prime[10010];
int prime[10010];
int t;
int n, m;
vector<int> map[10010];
int ans;
bool vis[10010];
struct Node{
	int x;
	int step;
};
bool isPrime(int n){
	for(int i = 2; i <= sqrt(n); i++){
		if(n % i == 0)
		return false;
	}
	return true;
}
void getPrime()
{
	for(int i=1000;i<10000;i++)
	{
		if(isPrime(i))
			prime[++prime[0]] = i;
	}

}
//如果只相差1个数字 就用邻接表添加
bool judge(int n, int m){
	int cnt = 0;
	while(n){
		if(n%10==m%10) cnt++;
		n /= 10;
		m /= 10;
	}
	if(cnt > 2) return true;
	return false;

}
void getMap(){
	for(int i = 1; i <= prime[0]; i++){
		for(int j = i +1; j <= prime[0];j++){
			if(judge(prime[i], prime[j])){
				map[prime[j]].push_back(prime[i]);
				map[prime[i]].push_back(prime[j]);
			}
		}
	}
}

int bfs(){
	memset(vis, false, sizeof(vis));
	queue<Node> q;
	Node a, b;
	a.step = 0;
	a.x = n;
	vis[a.x] = true;
	q.push(a);
	while(!q.empty()){
		a = q.front();
		q.pop();
		if(a.x == m){
			 return a.step;
		}
		for(int i = 0; i < map[a.x].size(); i++){
			int tem = map[a.x][i];
			if(!vis[tem]){
				vis[tem] = true;
				b.x = tem;
				b.step = a.step + 1;
				q.push(b);
			}
		}
	}
	return -1;
}
int main(){
	getPrime();
	getMap();
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n,&m);
		ans = bfs();
		if(ans == -1)
			cout << "Impossible" << endl;
		else cout << ans << endl;
	}
	return 0;
} 

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const int N = 100010;
int prime[N];
int cnt; //素数的个数
vector<int> connection[10010];//存与它关联的素数

bool checkPrime(int x)
{
	for(int i = 2; i <= sqrt(x); i++)
		if(x % i == 0)
			return false;
	return true;
} 

bool check(int x, int y)
{
	if(x == y)
		return false;
	int cnt = 0;
	while(x)
	{
		if(x % 10 != y % 10)
			cnt++;
		x /= 10;
		y /= 10;
	}
	if(cnt == 1)
		return true;
	return false;
}

void getPrime()
{
	for(int i = 1000; i <= 9999; i++)
		if(checkPrime(i))
			prime[++cnt] = i;
	
	for(int i = 1; i <= cnt; i++)
		for(int j = 1; j <= cnt; j++)
			if(check(prime[i], prime[j]))
				connection[prime[i]].push_back(prime[j]);
	
	/*for(int i = 1; i <= cnt; i++)
	{
		cout << prime[i] << " ";
		for(int j = 1; j < connection[prime[i]].size(); j++)
		{
			cout << connection[prime[i]][j] << " ";
		}
		cout <<endl;
	}*/
}

int k;
int n, m;
bool vis[10010];
typedef pair<int, int> PII;
queue<PII> q;
int bfs(int s, int e)
{
	memset(vis, false, sizeof vis);
	if(s == e)
		return 0;
	while(q.size())
		q.pop();
	
	q.push({s, 0});
	while(q.size())
	{
		PII t = q.front();
		q.pop();
		int x = t.first;
		int step = t.second;
		for(int i = 0; i < connection[x].size(); i++)
		{
			int y = connection[x][i];	
			if(y == e)
				return step + 1;
			if(!vis[y])
			{
				vis[y] = true;
				q.push({y, step + 1});
			}
		}
	}
	return -1;
}

int main()
{
	getPrime();
	cin >> k;
	while(k--)
	{
		cin >> n >> m;
		int ans = bfs(n , m);
		if(ans == -1)
			cout << "Impossible" <<endl;
		else cout <<ans <<endl;
		
	}

	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值