Uva 352 The Seasonal War

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 int IsVis[26][26];//记录位置是否被访问
 7 char Eagles[26][26];
 8 typedef struct node
 9 {
10     int x,y;
11 }War;
12 int Move[8][2]={-1,0, 0,1, 1,1, 1,0, -1,-1, 0,-1, 1,-1, -1,-1};//移动方位
13 int Bfs(int x,int y);
14 int line,EaglesNum;
15 int main(){
16
17     //freopen("D:\\t.txt","r",stdin);
18     while(cin>>line){
19         int flag = 1;
20         for(int i = 0;i< line;i++){
21                 for(int j = 0;j < line;j++){
22                     cin>>Eagles[i][j];
23                 }
24         }
25         EaglesNum = Bfs(0,0);
26         cout<<"Image number "<< flag<<" contains "<<EaglesNum<<" war eagles"<<endl;
27         flag++;
28     }
29
30     return 0;
31 }
32 int Bfs(int x,int y){
33     War m,n;
34     queue<War> Que;
35     int Num = 0;
36     m.x = x;
37     m.y = y;
38     memset(IsVis,0,sizeof(IsVis));//每一回合初始化
39     Que.push(m);
40     for(int i = 0;i< line;i++){
41         for(int j = 0;j < line;j++){
42             if(Eagles[i][j] == ‘1‘ && !IsVis[i][j]){
43                 while(!Que.empty()){
44                 m = Que.front();
45                 Que.pop();
46                 for(int k = 0;k < 8;k++){
47                     if((m.x + Move[k][0] > 0) && (m.x + Move[k][0] < line) && (m.y + Move[k][1] > 0) && (m.y + Move[k][1] < line)
48                        && !IsVis[m.x + Move[k][0]][m.y + Move[k][1]]){//判断移动是否超范围及是否被访问
49                         n.x = m.x + Move[k][0];
50                         n.y = m.y + Move[k][1];
51                         IsVis[n.x][n.y] = 1;
52                         if(Eagles[n.x][n.y] == ‘1‘){
53                             Que.push(n);
54                         }
55                     }
56                 }
57                 Num++;
58             }
59             }
60         }
61     }
62
63     return Num;
64 }

题目属于Bfs。

1.注意队列的使用;

2.注意输入以字符方式,不能用数字,用数字的话会错。

ps:uva挂了,不确定是否ac

时间: 2024-08-07 16:39:46

Uva 352 The Seasonal War的相关文章

UVA 之11729 - Commando War

There is a war and it doesn't look very promising for your country. Now it's time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-p

UVA - 10032 Tug of War (二进制标记+01背包)

Description Problem F: Tug of War A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must n

UVa 10032 - Tug of War

题目:有n个人分成两组,两组人数差不能超过1,找到两组的人重量之差的最小值. 分析:dp,状态压缩01背包.zoj1880升级版. 首先,考虑二维背包. 因为必须放在两个组中的一组,直接背包所有可到状态,取出相差不超过 1的最接近 sum/2的值即可. 然后,优化. 如果直接利用二维背包,由于数据组数较多会TLE,这里利用状态压缩的一维背包: 状态:f(i)表示总重为i时的所有可能的人数,f(i)的值,的第k位上的数是1则代表有k人的组合方式. 转移:f(i)= f(i)| (f(i-w[j])

暑期培训计划之个人计划

使用算法竞赛入门经典(刘汝佳编) 暑期培训计划之个人计划(7.22到8.13) 日期 周次         看书                                                      编程题目                                 看书完毕情况                        题目完毕情况         备注    2014.7.22 周二 第一章-第六章 (1-113页)  卡片游戏,简单枚举除法  完毕 完毕  2

使用maven多模块来构建系统时,spring初始化报错的问题

最近在实验maven结构的maven工程时,碰到一个问题,springbean总是初始化失败: Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\workspace\mavenweb\mavenweb-webapp\src\main\webapp\WEB-INF

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

uva 10158 War (并查集)

uva 10158 War 四个操作:1)使AB成为朋友.2)使AB成为敌人.3)询问AB是不是朋友.4)询问AB是不是敌人.PS:敌人的敌人是朋友.朋友的朋友是朋友. 开一个2 * n的数组,0~n - 1表示的是朋友,n~2 * n - 1表示的是n号国家的敌人. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib&

贪心 UVA 11729 Commando War

题目传送门 1 /* 2 贪心:按照执行时间长的优先来排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e3 + 10; 13

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y