码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 352 The Seasonal War

时间:2014-10-23 01:16:14      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   使用   for   sp   

 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

Uva 352 The Seasonal War

标签:style   blog   color   io   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/ohxiaobai/p/4044724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!