A
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 10;
bool bol[maxn];
char maps[maxn][maxn];
int n, k, cnt, way;
void dfs(int h)
{
if(cnt == k)
{
way++;
return ;
}
if(h >= n)
return ;
for(int i = 0; i < n; i++)
{
if(!bol[i] && maps[h][i] == '#')
{
cnt++;
bol[i] = true;
dfs(h + 1);
cnt--; //dfs 初始化操作
bol[i] = false;
}
}
dfs(h + 1);
}
int main()
{
while(1)
{
way = 0;
cnt = 0;
memset(bol, false, sizeof bol);
cin >> n >> k;
if(n == -1 && k == -1)
break;
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
cin >> maps[i][j];
}
}
dfs(0);
cout << way << endl;
}
return 0;
}
B
#include <iostream>
#include <cmath>
using namespace std;
int n, a = 1, b = 0, ans = 0x3f3f3f3f;
int S[20];
int B[20];
bool vis[20];
void dfs(int x)
{
if(x > n)
return ;
else
{
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
a *= S[i];
b += B[i];
vis[i] = 1;
ans = min(ans, abs(a - b));
dfs(i + 1);
vis[i] = 0;
a /= S[i];
b -= B[i];
}
}
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> S[i] >> B[i];
}
dfs(1);
cout << ans << endl;
return 0;
}
C
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 10;
char str[maxn];
int main()
{
cin >> str;
int n = strlen(str);
do
{
cout << str << endl;
}
while(next_permutation(str, str + n));
return 0;
}
D
#include <iostream>
using namespace std;
int n, a[30];
void pr(int x)
{
cout << n << '=';
for(int i = 1; i < x - 1; i++)
{
cout << a[i] << '+';
}
cout << a[x - 1] << endl;
}
void dfs(int x, int y) // x 表示 第几个盒子,y表示当前数字还剩多少
{
if(y == 0 && x > 2) // 01 - 为什么 x > 2 至少拆一次 即x > 1 但默认会x + 1 所以 x > 2
{
pr(x);
return ;
}
for(int i = 1; i <= y; i++)
{
if(i >= a[x - 1]) // 后面的数字要大于等于前面的数字
{
a[x] = i;
y -= i;
dfs(x + 1, y);
a[x] = 0;
y += i;
}
}
}
int main()
{
cin >> n;
a[0] = 1;
dfs(1, n);
return 0;
}
E
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
int ans[40];
bool vis[40];
void pr()
{
for(int i = 1; i <= n - 1; i++)
{
cout << ans[i] << ' ';
}
cout << ans[n] << endl;
}
bool isPrime(int x)
{
bool flag = true;
for(int i = 2; i <= sqrt(x); i++)
{
if(x % i == 0)
flag = false;
}
return flag;
}
void dfs(int step)
{
if(step > n && isPrime(ans[1] + ans[n]))
{
pr();
return ;
}
for(int i = 2; i <= n; i++)
{
ans[step] = i;
if(vis[i] || isPrime(ans[step] + ans[step - 1]) == 0)
continue;
vis[i] = 1;
dfs(step + 1);
vis[i] = 0;
}
}
int main()
{
int j = 1;
while(cin >> n)
{
if(j >= 2)
cout << endl;
memset(ans, 0, sizeof ans);
memset(vis, 0, sizeof vis);
printf("Case %d:\n", j);
j++;
vis[1] = 1;
ans[1] = 1;
dfs(2);
}
return 0;
}
F
#include <iostream>
#include <cstring>
using namespace std;
int n, m, way, sx, sy;
char a[30][30];
bool map[30][30];
bool vis[30][30];
int xx[ ] = {0, 0, -1, 1};
int yy[ ] = {1, -1, 0, 0};
void dfs(int x, int y)
{
for(int i = 0; i < 4; i++)
{
int dx = x + xx[i];
int dy = y + yy[i];
if(map[dx][dy] || vis[dx][dy] || dx < 1 || dx > n || dy < 1 || dy > m)
continue;
way++;
vis[dx][dy] = 1;
dfs(dx, dy);
// vis[dx][dy] = 0; // 不回溯
}
}
int main()
{
while(1)
{
way = 1;
cin >> m >> n;
if(m == 0 && n == 0)
break;
memset(a, '\0', sizeof a);
memset(map, false, sizeof map);
memset(vis, false, sizeof vis);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == '#')
map[i][j] = true;
if(a[i][j] == '@')
{
sx = i;
sy = j;
}
}
}
vis[sx][sy] = 1;
dfs(sx, sy);
cout << way << endl;
}
return 0;
}
G
#include <bits/stdc++.h>
#define MAXN 305
using namespace std;
int sx, sy, ex, ey, l;
bool vis[MAXN][MAXN];
int moves[8][2] = {{-2, -1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
struct node
{
int x;
int y;
int step;
} p[1000050];
void bfs()
{
memset(vis, false, sizeof vis);
vis[sx][sy] = 1;
int head = 1, tail = 1;
p[tail].x = sx;
p[tail].y = sy;
p[tail].step = 0;
tail++;
while(head < tail)
{
int x = p[head].x;
int y = p[head].y;
int step = p[head].step;
if(x == ex && y== ey)
{
cout << step << endl;
return ;
}
for(int i = 0; i < 8; i++)
{
int nx = x + moves[i][0];
int ny = y + moves[i][1];
if(nx >= 0 && nx < l && ny >= 0 && ny < l && vis[nx][ny] == 0)
{
vis[nx][ny] = 1;
p[tail].x = nx;
p[tail].y = ny;
p[tail].step = step + 1;
tail++;
}
}
head++;
}
}
int main()
{
int n;
cin >> n;
while(n--)
{
cin >> l >> sx >> sy >> ex >> ey;
bfs();
}
return 0;
}
H
#include <iostream>
using namespace std;
int n, m, way;
bool vis[200][200];
char map[200][200];
int moves[8][2] = {{1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}};
void dfs(int x, int y)
{
map[x][y] = '*';
for(int i = 0; i < 8; i++)
{
int dx = x + moves[i][0], dy = y + moves[i][1];
if(map[dx][dy] == '@' && dx >= 0 && dx <= n && dy >= 0 && dy <= m)
dfs(dx, dy);
}
}
int main()
{
int i, j, k = 0;
int u, v;
char ch;
while(cin >> n >> m)
{
int ans = 0;
if(n == 0 && m == 0)
break;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
cin >> map[i][j];
}
}
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
if(map[i][j] == '@')
{
dfs(i, j);
ans++;
}
}
}
cout << ans << endl;
}
return 0;
}
I
#include <iostream>
using namespace std;
int n, m, way;
char map[150][150];
bool vis[150][150];
int moves[8][2] = {{1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}};
void dfs(int x, int y)
{
map[x][y] = '.';
for(int i = 0; i < 8; i++)
{
int dx = x + moves[i][0];
int dy = y + moves[i][1];
if(map[dx][dy] == 'W' && dx <= n && dx >= 1 && dy <= m && dy >= 1)
{
vis[dx][dy] = 1;
dfs(dx, dy);
}
}
}
int main()
{
int ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> map[i][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(map[i][j] == 'W')
{
dfs(i, j);
ans++;
}
}
}
cout << ans << endl;
return 0;
}
J
#include<bits/stdc++.h>
using namespace std;
struct p{
int l;
int r;
}t[100010];
void dfs(int x){
if(x==0)
return;
cout<<x<<endl;//第1步
dfs(t[x].l);//第2步
dfs(t[x].r);//第3步
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>t[i].l>>t[i].r;//依次读入左右节点
dfs(1);
return 0;
}
K
#include <iostream>
using namespace std;
int n, m, sx, sy, tx, ty, flag = 0;
char map[20][20];
bool vis[20][20];
bool wal[20][20];
int xx[] = {1,0,-1,0};
int yy[] = {0,-1,0,1};
void dfs(int x,int y)
{
if(x == tx && y == ty)
{
flag = 1;
return ;
}
for(int i = 0; i < 4; i++)
{
int dx = x + xx[i];
int dy = y + yy[i];
if(dx >= 1 && dx <= n && dy >= 1 && dy <= m && !vis[dx][dy] && !wal[dx][dy])
{
vis[dx][dy] = 1;
dfs(dx, dy);
vis[dx][dy] = 0;
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> map[i][j];
if(map[i][j] == '*')
wal[i][j] = 1;
if(map[i][j] == 'S')
sx = i, sy = j;
if(map[i][j] == 'T')
tx = i, ty = j;
}
}
vis[sx][sy] = 1;
dfs(sx, sy);
if(flag == 0)
cout << "no" << endl;
else
cout << "yes" << endl;
return 0;
}
L
#include <iostream>
#include <cstring>
using namespace std;
int t, n, m, x, y, ans = 0;
int xx[ ] = {-2, -1, 1, 2, 2, 1, -1, -2};
int yy[ ] = {1, 2, 2, 1, -1, -2, -2, -1};
bool vis[10][10];
void dfs(int x, int y, int step)
{
if(step == n * m)
{
ans++;
return ;
}
else
{
for(int i = 0; i < 8; i++)
{
int dx = x + xx[i];
int dy = y + yy[i];
if(vis[dx][dy] || dx < 0 || dx >= n || dy < 0 || dy >= m)
continue;
vis[dx][dy] = 1;
dfs(dx, dy, step + 1);
vis[dx][dy] = 0;
}
}
}
int main()
{
cin >> t;
while(t--)
{
ans = 0;
memset(vis, false, sizeof vis);
cin >> n >> m >> x >> y;
vis[x][y] = 1;
dfs(x, y, 1);
cout << ans << endl;
}
return 0;
}
M
#include<bits/stdc++.h>
using namespace std;
const int n = 8;
int tmp, ans;
int x[10];
int chess[10][10];
bool no(int r, int c) {//判断两个皇后的坐标是否冲突
for (int i = 0; i < r; i++) {
if (x[i] == c || abs(r - i) == abs(c - x[i]))
return false;
}
return true;
}
void queen(int k) {
if (k == n) {
ans = max(ans, tmp);//找所有情况的八皇后的最大权值和
return;
}
for (int i = 0; i < n; i++) {
if (no(k, i)) {
x[k] = i;//dfs前做好标记
tmp += chess[k][i];
queen(k + 1);
x[k] = 0;//dfs后删除标记
tmp -= chess[k][i];
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
tmp = ans = 0;
memset(x, 0, sizeof(x));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
scanf("%d", &chess[i][j]);
}
queen(0);
printf("%d\n", ans);
}
return 0;
}
N
#include <iostream>
#include <cmath>
using namespace std;
int ans, m, n, k, a[30];
bool isPrime(int x)
{
int i;
for(i = 2; i <= sqrt(x); i++)
{
if(x % i == 0)
return 0;
}
return 1;
}
void dfs(int m, int sum, int j)
{
if(m == k)
{
if(isPrime(sum))
ans++;
return ;
}
for(int i = j; i < n; i++)
{
dfs(m + 1,sum + a[i], i + 1);
}
return ;
}
int main()
{
cin >> n >> k;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
dfs(0, 0, 0);
cout << ans << endl;
return 0;
}
O
#include<bits/stdc++.h>
using namespace std;
#define fir(i,a,b) for(int i=a;i<=b;i++)
const int N=510;
const int dxy1[4][2]= {{1,1},{-1,-1},{1,-1},{-1,1}},dxy2[4][2]= {{1,1},{0,0},{1,0},{0,1}};
struct node
{
int x,y;
};
int t,n,m,ans=1e8,dis[N][N];
char s[N][N];
bool vis[N][N];
deque<node> q;
int check(int x,int y)
{
return x>=0 && x<=n && y>=0 && y<=m;
}
void bfs()
{
vis[0][0]=1;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
q.push_front(node {0,0});
dis[0][0]=0;
while(q.size())
{
node now=q.front();
q.pop_front();
fir(i,0,3)
{
int tx=now.x+dxy1[i][0],t1=now.x+dxy2[i][0];
int ty=now.y+dxy1[i][1],t2=now.y+dxy2[i][1];
int tt=(s[t1][t2] != (i<=1? '\\':'/'));
if(check(tx,ty) && dis[tx][ty]>dis[now.x][now.y]+tt)
{
dis[tx][ty]=dis[now.x][now.y]+tt;
if(tt)
q.push_back(node {tx,ty});
else
q.push_front(node {tx,ty});
}
}
}
}
int main()
{
cin>>n>>m;
fir(i,1,n)
fir(j,1,m)
cin>>s[i][j];
bfs();
if(dis[n][m]<1e8)
cout<<dis[n][m]<<endl;
else
cout<<"NO SOLUTION"<<endl;
return 0;
}
943

被折叠的 条评论
为什么被折叠?



