26. 扫雷游戏提交
提交排名
题目描述
扫雷游戏是一款十分经典的单机小游戏。在n行m列的雷区中有一些格子含有地雷(称之为地雷格),其他格子不含地雷(称之为非地雷格)。玩家翻开一个非地雷格时,该格将会出现一个数字——提示周围格子中有多少个是地雷格。游戏的目标是在不翻出任何地雷格的条件下,找出所有的非地雷格。 现在给出n行m列的雷区中的地雷分布,要求计算出每个非地雷格周围的地雷格数。 1≤n≤100, 1≤m≤100。
输入输出格式
输入格式
输入文件第一行是用一个空格隔开的两个整数n和m,分别表示雷区的行数和列数。 接下来n行,每行m个字符,描述了雷区中的地雷分布情况。字符’*’表示相应格子是地雷格,字符’?’表示相应格子是非地雷格。相邻字符之间无分隔符。
输出格式
输出文件包含n行,每行m个字符,描述整个雷区。用’*’表示地雷格,用周围的地雷个数表示非地雷格。相邻字符之间无分隔符
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
char f[101][101];
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>f[i][j];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(f[i][j]!='*')f[i][j]='0';
if(f[i][j]!='*'&&f[i+1][j+1]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i+1][j]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i+1][j-1]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i-1][j+1]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i-1][j]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i-1][j-1]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i][j+1]=='*')f[i][j]++;
if(f[i][j]!='*'&&f[i][j-1]=='*')f[i][j]++;}}
for(int i=1;i<=n;i++,cout<<endl)
for(int j=1;j<=m;j++)
cout<<f[i][j];
return 0;
}
27. 工作分配提交
提交排名
题目描述
设有m件工作分配给n个人。如果第i个人被分配到工作j所需费用为c[i][j]。请为每一个人都分配一件不同的工作,并使总费用达到最小。
输入输出格式
输入格式
输入文件work.in 第一行有2个正整数n, m(1≤n≤m≤20)。接下来的n行,每行m个数,第i行表示第i个人各项工作费用,数字不超过100000
输出格式
输出文件work.out 输出一行,即最小总费用
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
int n,m,c[N][N];
bool vis[N];
int ans=INT_MAX;
void dfs(int k, int sum){
if(k>n){
ans=min(sum, ans);
return;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
vis[i]=true;
dfs(k+1,sum+c[k][i]);
vis[i]=false;
}
}
}
int main(){
freopen("work.in","r",stdin);
freopen("work.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d", &c[i][j]);
dfs(1,0);
cout<<ans;
return 0;
}
28. 逃离迷宫提交
提交排名
题目描述
设有一个N*N(2<=N<10)的迷宫,入口和出口分别在左上角和右上角。迷宫用0,1表示,0表示通道,1表示障碍。每一点有八个方向可走(上、下、左、右、左上、左下、右上、右下)。找出所有从入口(左上角)到出口(右上角)的路径(不能重复),输出路径总数,如果无法到达,则输出0。
输入输出格式
输入格式
第一行整数n,表示迷宫大小 接下来n行,每行n列,均为0或1,表示迷宫
输出格式
全部的走迷宫方案数。如果不存在,输出0
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int sy[8] = {1, -1, 0, 1, -1, 0, 1, -1};
int n, ans = 0;
int a[100][100], b[100][100];
void dfs(int s, int t)
{
int x, y;
if (s == 1 && t == n)
{
ans++;
return;
}
for (int i = 0; i < 8; i++)
{
x = s + sx[i];
y = t + sy[i];
if (x > 0 && y > 0 && x <= n && y <= n && a[x][y] == 0 && b[x][y] == 0)
{
b[x][y] = 1;
dfs(x, y);
b[x][y] = 0;
}
}
return;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
memset(b, 0, sizeof(b));
b[1][1] = 1;
dfs(1, 1);
cout << ans;
return 0;
}
29. 卡片排列提交
提交排名
题目描述
现有四张卡片(数字0-9),用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
输入输出格式
输入格式
一行4个正整数,代表四张卡片上的数字(0<=数字<=9)
输出格式
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。老师备注:行末数字后面不能输出空格,否则判错
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stdio.h>
using namespace std;
int main()
{
int a[5], t[5], tag = 0, sum = 0;
while (cin >> a[0] >> a[1] >> a[2] >> a[3])
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4 - i - 1; j++)
{
if (a[j] > a[j + 1])
{
t[i] = a[j];
a[j] = a[j + 1];
a[j + 1] = t[i];
}
}
if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
break;
if (tag)
printf("\n");
tag = 1;
int flag = 1, tmp;
do
{
if (a[0] == 0)
continue;
if (flag)
{
printf("%d%d%d%d", a[0], a[1], a[2], a[3]);
flag = 0;
}
else if (tmp == a[0])
printf(" %d%d%d%d", a[0], a[1], a[2], a[3]);
else
printf("\n%d%d%d%d", a[0], a[1], a[2], a[3]);
tmp = a[0];
} while (next_permutation(a, a + 4));
printf("");
break;
}
return 0;
}
30. 子集和提交
提交排名
题目描述
给定正整数a1,a2,…, an,是否可以从中选出若干个数,使它们的和恰好等于m
输入输出格式
输入格式
第1行有2个正整数n和m,n表示整数的个数,m是要求得的和。第2行是n个正整数
输出格式
如果存在选择的方案,输出YES,否则输出NO
#include <iostream>
#define N 10005
using namespace std;
long long n, m, a[N], i, j, k, ans, cnt, x, he;
int main()
{
cin >> n >> m;
for (i = 0; i < n; i++)
{
cin >> x;
a[i] = x;
he += x;
if (x == m)
{
cout << "YES";
return 0;
}
}
for (j = 0; j < n; j++)
{
cnt = a[j];
if (he - a[j] == m)
{
cout << "YES";
return 0;
}
for (k = 0; k != j && k < n; k++)
{
ans = a[j] + a[k];
if (ans == m)
{
cout << "YES";
return 0;
}
if (he - ans == m)
{
cout << "YES";
return 0;
}
cnt += a[k];
if (cnt == m)
{
cout << "YES";
return 0;
}
if (he - cnt == m)
{
cout << "YES";
return 0;
}
}
cnt = ans = 0;
}
cout << "NO";
return 0;
}
看一眼吧,球球了