马踏棋盘

马踏棋盘

https://ac.nowcoder.com/acm/contest/59977/1071

思路:dfs或者bfs均可

bfs解法:

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

const int N = 100010;
typedef long long ll;
typedef pair<int,int> PII;

int n,m;
int ans = 0;
int l[5] = {1,2,1,2};
int r[5] = {2,1,-2,-1};

void bfs(){
	queue<PII> q;
	q.push({1,1});
	while(!q.empty()){
		auto t = q.front();
		q.pop();
//		cout << t.first << ' ' <<t.second << '\n';
		if(t.first == m && t.second == n){
			ans ++;
		}
		else{
			for(int i = 0; i < 4; i ++){
				int x1 = t.first + l[i];
				int y1 = t.second + r[i];
				if(x1 >= 1 && x1 <= m && y1 >= 1 && y1 <= n){
					q.push({x1,y1});
				}
			}
//			cout << '\n';
		}
	}

}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	bfs();
	cout << ans;
	return 0;
}

dfs解法

#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 110;
const int inf = INT_MAX;
typedef pair<int, int> PII;
/*---------------------------------------------------------------------------*/
int n, m;
bool st[N][N] = {false};
int ans;

int dx[] = {1, 1, 2, 2}, dy[] = {2, -2, 1, -1};

void dfs (int x, int y) {
	if (x == m && y == n) {
		ans ++;
		return;
	}
 	cout << x << " " << y << endl;
	st[x][y] = true;

	for (int i = 0; i < 4; i ++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 1 && nx <= m && ny >= 1 && ny <= n && !st[nx][ny]) {
			dfs (nx, ny);
		}
	}
		st[x][y] = false;

}

void solve(){
	cin >> n >> m;
	dfs (1, 1);
	cout << ans;
}
/*---------------------------------------------------------------------------*/
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
//  int tt; cin >> tt; while(tt --) solve();
    solve();
    return 0;
}
全部评论

相关推荐

找个工作&nbsp;学历是要卡的&nbsp;要求是高的&nbsp;技能不足是真的&nbsp;实习经验是0的&nbsp;简历无处可写是事实的&nbsp;钱不好赚是真的&nbsp;想躺平又不敢躺&nbsp;也不甘心躺&nbsp;怕自己的灵感和才华被掩埋甚至从未被自己发现&nbsp;又质疑自己是否真正有才华
码农索隆:你现在啊,你心里都明白咋回事,但是你没办法改变现状,一想到未来,你又没有信心狠下心来在当下努力。 得走出这种状态,不能一直困在那里面,哪不行就去提升哪,你一动不动那指定改变不了未来,动起来,积少成多才能越来越好
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
fRank1e:吓得我不敢去外包了,但是目前也只有外包这一个实习,我还要继续去吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务