**
描述
给定整数a1、a2、…an,判断是否可以从中选出若干数,使它们的和恰好为K。
输入
首先,n和k,n表示数的个数,k表示数的和。
接着一行n个数。(1<=n<=20,保证不超int范围)**
输出
#include
using namespace std;
const int maxn = 25;
int a[maxn];
int n, k;
bool dfs(int i, int sum)
{
if (i == n)return sum == k;
if (dfs(i + 1, sum))
return true;
if (dfs(i + 1, sum + a[i]))
return true;
return false;//如果加上a[i]和不加上a[i]都不满足则返回false
}
int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cin >> k;
if (dfs(0, 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
#include
using namespace std;
const int maxn = 25;
int a[maxn];
typedef long long LL;
int n, k;
int vis[maxn];
/*
4
1 2 4 7
13
*/
int flag,s=0;
void dfs(int m)
{
if (s == k)
{
if (flag == 0)flag = 1;
if (flag){
cout << "Yes" << endl;
for (int i = 0; i < n; i++)
{
if (vis[i] == 1)
cout << a[i]<<" ";
}
cout << endl;
}
}
for (int i = m; i < n; i++)
{
s += a[i];//找到s+a[i]等于k的
vis[i] = 1;
dfs(i + 1);
s -= a[i];//没找到就减去a[i],继续遍历
vis[i] = 0;//并初始化访问
}
return;
}
int main()
{
while (cin >> n)
{
flag = 0;
memset(a, 0, sizeof(a));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> k;
dfs(0);
if (flag == 0)
{
cout << "No" << endl;
}
}
return 0;
}
Lake Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 53321 Accepted: 26073
Description
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (’.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
Input
本题是要你找出水洼个数W,并且W的上,下,左,右,及左上,左下,右上,右下,这八个部分表示同一个水洼,我们只需用深度优先搜索,搜索它的这些部分的所有W,然后搜索次数则为遍历次数,每搜索到一个W时则让这个W变为’ . '。
#include
using namespace std;
const int maxn = 105;
char field[maxn][maxn];
int n, m;
void dfs(int x, int y)
{
field[x][y] = '.';//将当前的w变为点
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
int nx = x + dx; int ny = y + dy;
if (nx >= 0 && nx < n&&ny >= 0 && ny < m&&field[nx][ny] == 'W')
{
dfs(nx, ny);
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> field[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (field[i][j] == 'W')
{
dfs(i, j);
ans++;
}
}
}
cout << ans << endl;
return 0;
}
Fox Ciel is playing a mobile puzzle game called “Two Dots”. The basic levels are played on a board of size n × m cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, …, dk a cycle if and only if it meets the following condition:
These k dots are different: if i ≠ j then di is different from dj.
1.k is at least 4.
2.All dots belong to the same color.
3.For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.
Determine if there exists a cycle on the field.
Examples
Input
3 4
AAAA
ABCA
AAAA
Output
Yes
Input
3 4
AAAA
ABCA
AADA
Output
No
Input
4 4
YYYR
BYBY
BBBY
BBBY
Output
Yes
Input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
Output
Yes
Input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
Output
No
Note
In first sample test all ‘A’ form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above (‘Y’ = Yellow, ‘B’ = Blue, ‘R’ = Red).
#include
#include
using namespace std;
const int maxn = 50;
char a[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {
{
1, 0 }, {
-1, 0 }, {
0, 1 }, {
0, -1 } };
int flag;
int ex, ey;
int nx, ny;
int n, m;
void dfs(int x, int y,int n1)
{
if (flag)return;
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
//如果初始的坐标与最后的坐标相等且大于三个相同字母flag=1
if (ex==nx&&ey==ny&& n1 > 2)
{
//n1从零开始计数大于二即可
flag = 1;
return;
}
nx = x + dir[i][0];
ny = y + dir[i][1];
if (!vis[nx][ny]&&nx >= 0 && nx < n&&ny >= 0 && ny <m&&a[x][y] == a[nx][ny])
dfs(nx, ny, n1 + 1);
}
}
int main()
{
while (cin >> n >> m)
{
flag = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
for (int i = 0; i < n&&!flag; i++)
{
for (int j = 0; j < m&&!flag; j++)
{
memset(vis, 0, sizeof(vis));//每次搜索完成初始化vis
ex = i, ey = j;
dfs(i, j, 0);
}
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}