今天写难题写的自闭了心血来潮,然后就复习一下DFS&BFS
然后上来第一题n皇后 wdnmd,又t了三遍,一看题解nmd打表。。。我佛了、
n皇后
//
// main.cpp
// n皇后_复习
//
// Created by 陈冉飞 on 2019/8/16.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
int n,a[15],cnt;
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
bool ok(int c){
for (int i = 1; i < c; i++)
if (a[i]-a[c] == c-i || a[i]-a[c] == i-c || a[i] == a[c]) return false;
return true;
}
void dfs(int c){
if (c == n+1) {cnt++;return;}
for (int i = 1; i <= n; i++) {
a[c] = i;
if (ok(c)) dfs(c+1);
}
}
int ans[15];
int main(int argc, const char * argv[]) {
for (int y = 1; y <= 10; y++) {
cnt = 0;n = y;
dfs(1);
ans[y] = cnt;
}
while (scanf("%d",&n) && n) {
// cnt = 0;cl(a, 0);
// dfs(1);
printf("%d\n",ans[n]);
// cout<<cnt<<endl;
}
return 0;
}
复习DFS就是爽,原来死也de不对的bug现在tmd1A,做梦都不敢想虽然是水中水中水的题
poj3620![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bbb98489a5544c0b3a0174cbfe50d966.png)
//
// main.cpp
// 找相邻的_DFS
//
// Created by 陈冉飞 on 2019/8/16.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
#define maxn 105
int n,m,k,a[maxn][maxn],x,y,vis[maxn][maxn],cnt,ans = 0;
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
int c[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y){
for (int i = 0; i < 4; i++) {
x += c[i][0];y += c[i][1];
if(a[x][y] && !vis[x][y]) {
cnt++;
vis[x][y] = 1;
dfs(x, y);
}
x -= c[i][0];y -= c[i][1];
}
}
int main(int argc, const char * argv[]) {
scanf("%d%d%d",&n,&m,&k);
for (int i = 1; i <= k; i++) {
scanf("%d%d",&x,&y);
a[x][y] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
cnt = 0;
if (a[i][j] && !vis[i][j]) dfs(i,j);
ans = max(cnt, ans);
}
cout<<ans<<endl;
return 0;
}
zoj1709
然后是dfs求区块的块数,(就是上边的求区块成员最多的变式)竟然ce了一发,。。。
//
// main.cpp
// DFS_求连通块_zoj1709
//
// Created by 陈冉飞 on 2019/8/17.
// Copyright © 2019 陈冉飞. All rights reserved.
//
//#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define maxn 105
char a[maxn][maxn];
int vis[maxn][maxn];
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
int n,m,ans;
int c[8][2] ={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
void dfs(int x,int y){
for (int i = 0; i < 8; i++) {
x += c[i][0];y += c[i][1];
if (a[x][y] == '@' && !vis[x][y]) {
vis[x][y] = 1;
dfs(x, y);
}
x -= c[i][0];y -= c[i][1];
}
}
int main(int argc, const char * argv[]) {
while (scanf("%d%d",&n,&m) && n+m) {
cl(vis,0);ans = 0;
for (int i = 0; i < n; i++) scanf("%s",a[i]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] == '@' && !vis[i][j]) {ans++;dfs(i,j);}
cout<<ans<<endl;
}
return 0;
}
8.18
BFS 迷宫+路径
A水题找自信,又1A了一道水题
poj3984
水题:迷宫问题打印一个路径,思路加个二维数组标记一下这个节点的父节点是谁,然后到达(4,4)之后履着每个节点的父节点往回找,然后用个容器储存起来倒着输出即可(在这里用的vector<>开了个ans,来记录。)
//
// main.cpp
// BFS_迷宫_poj3984
//
// Created by 陈冉飞 on 2019/8/18.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
#define maxn 6
#include <queue>
struct node {
int x,y;
};
node fa[maxn][maxn];
int vis[maxn][maxn],a[maxn][maxn];
queue<node>q;
vector<node>ans;//最后将顺序调过来的向量
int c[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void print_node(){
node f;f.x = 4;f.y = 4;
ans.push_back(f);
while (true) {
ans.push_back(fa[f.x][f.y]);
if (f.x == 0 && f.y == 0) break;
f = fa[f.x][f.y];
}
for (int i = ans.size()-2; i >=0; i--) printf("(%d, %d)\n",ans[i].x,ans[i].y);
}
void bfs(){
vis[0][0] = 1;
node f;f.x = 0;f.y = 0;q.push(f);
while (!q.empty()) {
node n = q.front();q.pop();
for (int i = 0; i < 4; i++) {
int ntx = n.x+c[i][0],nty = n.y+c[i][1];
if (!a[ntx][nty] && !vis[ntx][nty] && ntx <=4 && ntx >= 0 && nty <= 4 &&nty >= 0) {
// cout<<ntx<<" "<<nty<<endl;
vis[ntx][nty] = 1;
fa[ntx][nty] = n;
if (ntx == 4 && nty == 4) {
print_node();
return;
}
node s;s.x = ntx;s.y = nty;
q.push(s);
}
}
}
}
int main(int argc, const char * argv[]) {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
scanf("%d",&a[i][j]);
bfs();
return 0;
}