CSP初赛复习-27-搜索与回溯-练习题
CSP初赛复习-27-搜索与回溯-练习题
PDF及答案公众号回复关键字:CSPC27
阅读程序
#include <iostream>
using namespace std;
int n,i,j,a[100][100];
int solve(int x,int y)
{
int u,v;
if(x==n) return a[x][y];
u=solve(x+1,y);
v=solve(x+1,y+1);
if(u>v) return a[x][y]+u;
else return a[x][y]+v;
}
int main()
{
cin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++) cin>>a[i][j];
cout<<solve(1,1)<<endl;
return 0;
}
/*
输入
5
2
-1 4
2 -1 -2
-1 6 4 0
3 2 -1 5 8
输出:
?
*/
2
#include <stdio.h>
#include <string.h>
#define SIZE 100
int n, m, p, count;
int a[SIZE][SIZE];
void colour(int x, int y)
{
count++;
a[x][y] = 1;
if ((x > 1) && (a[x - 1][y] == 0))
colour(x - 1, y);
if ((y > 1) && (a[x][y - 1] == 0))
colour(x, y - 1);
if ((x < n) && (a[x + 1][y] == 0))
colour(x + 1, y);
if ((y < m) && (a[x][y + 1] == 0))
colour(x, y + 1);
}
int main()
{
int i, j, x, y, ans;
memset(a, 0, sizeof(a));
scanf("%d%d%d", &n, &m, &p);
for (i = 1; i <= p; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
ans = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (a[i][j] == 0) {
count = 0;
colour(i, j);
if (ans < count)
ans = count;
}
printf("%d\n", ans);
return 0;
}
/*
输入:
6 5 9
1 4
2 3
2 4
3 2
4 1
4 3
4 5
5 4
6 4
输出:
?
*/
作者:newcode 更多资源请关注纽扣编程微信公众号

从事机器人比赛、机器人等级考试、少儿scratch编程、信息学奥赛等研究学习

浙公网安备 33010602011771号