| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 538 人关注过本帖
标题:一道 HDU 的题,疑问在代码里面。为什么要用|| 而不用&&呢??
只看楼主 加入收藏
linxwu
Rank: 1
等 级:新手上路
帖 子:21
专家分:2
注 册:2014-2-28
结帖率:25%
收藏
 问题点数:0 回复次数:0 
一道 HDU 的题,疑问在代码里面。为什么要用|| 而不用&&呢??
原文链接:http://acm.hdu.
<span style="font-size:24px;">/*算法思路:从字符数组oil的第一元素开始,遍历整个数组,
当遇到'@’时,进入队列push,立即更新此字符为‘* ’,调用bfs(),
while循环,结束条件是队列为空,出列pop,在此字符上的8个方向上进行一次遍历,
并入列,且及时更新该字符为‘* ’     */
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int r,c,dx[8] = {0,1,1,1,0,-1,-1,-1},dy[8] = {1,1,0,-1,-1,-1,0,1};
char oil[101][101];
typedef pair<int,int> P;
P p;//使用pair类型存储每一个字符所在的坐标:横坐标 p.first 纵坐标p.second
queue<P> que; //队列的元素是pair类型

//广度优先搜索
void bfs()
{
    while(!que.empty())  // que.empty()队列为空返回真值
    {
          p = que.front();
        que.pop();
        int xx = p.first;
        int yy = p.second;
        
        int i,j,x1,y1;
        for(i = 0;i < 8;i++)//8个方向  
        {
            x1 = xx + dx[i];
            y1 = yy + dy[i];
            if(x1 >= 0 || x1 < c || y1 >= 0 || y1 < r)//x1不能超过列坐标,y1不能超过行坐标 /////////////////
            //if(x1 >= 0 && x1 < c && y1 >= 0 && y1 < r)  //我对这个有疑问,为什么不能用&& /////////////////
            {
               
                if(oil[x1][y1] == '@')
                {
                    oil[x1][y1] = '*';//更新已访问过的值,避免下次的访问
                    p.first = x1;
                    p.second = y1;
                    que.push(p);
                }
            }   
        }
    }
   
}
int main(int argc, char *argv[])
{
    while(1)
    {
        int r,c;
        cin >> r >> c;
        if(r == 0 && c == 0)
            return 0;
            
        int i,j;
        for(i = 0;i < r;i++)
            for(j = 0;j < c;j++)
                    cin >> oil[i][j];
        
        int count = 0;   
        for(i = 0;i < r;i++)
        {
            for(j = 0;j < c;j++)
            {
                if(oil[i][j] == '@')
                {
                    count++;
                    oil[i][i] = '*';//更新已访问过的值,避免下次的访问
                    p.first = i;
                    p.second = j;
                    que.push(p);
                    bfs();
                }   
            }
        }   
        cout << count << endl;   
    }
    return 0;
}</span>
搜索更多相关主题的帖子: include style 元素 
2014-11-16 22:41
快速回复:一道 HDU 的题,疑问在代码里面。为什么要用|| 而不用&&呢?? ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034011 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved