| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 742 人关注过本帖
标题:以下是我自己编的代码,但是找不出问题所在,大神求解
只看楼主 加入收藏
uuien22
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-2-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
以下是我自己编的代码,但是找不出问题所在,大神求解
题目:

Description
Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father's will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.
For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.
Now write a program to find the number of cells (unit land) he could reach including the cell he was living.


Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.
1) '.' - land
2) '#' - water
3) '@' - initial position of prince (appears exactly once in a dataset)

Output
For each case, print the case number and the number of cells he can reach from the initial position (including it).

Sample Input
4
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..

Sample Output
Case 1: 45
Case 2: 59
Case 3: 6
Case 4: 13

以下是我的代码:  
程序代码:
#include <iostream>
using namespace std;
int t=0,r,k;
int pr( char (*a)[20] ,int n,int m)
{
    if(a[n][m]=='@') {t++;return 0;}
    if(a[n][m+1]=='.' && m+1<=k)
        if(pr(a,n,m+1)==0) return 0;
    if(a[n][m-1]=='.' && m-1>=0)
        if(pr(a,n,m-1)==0) return 0;
    if(a[n-1][m]=='.' && n-1>=0)
        if(pr(a,n-1,m)==0) return 0;
    if(a[n+1][m]=='.' && n+1<=r)
        if(pr(a,n+1,m)==0) return 0;
    if(a[n][m+1]=='@' && m+1<=k) {t++;return 0;}
    if(a[n][m-1]=='@' && m-1>=0) {t++;return 0;}
    if(a[n-1][m]=='@' && n-1>=0) {t++;return 0;}
    if(a[n+1][m]=='@' && n+1<=r) {t++;return 0;}
    return 0;
}
int num(int a[],int i)
{
    if (i==0) return 0;
    return a[i-1]+num( a ,i-1);
}
int main ()
{
    int n,m,w[20]={0},i,T;
    char a[20][20];
    cin >> T;
    for( i=0;i<T;i++)
    {
        cin >> r >> k;
        for(n=0;n<k;n++)
            for(m=0;m<r;m++)
                cin >> a[n][m];
        for(n=0;n<k;n++)
            for(m=0;m<r;m++)
                pr(a,n,m);
        w[i]=t-num(w,i);
    }
    for (r=0;r<T;r++)
    {
        cout << w[r] << endl;
    }
    return 0;
}

搜索更多相关主题的帖子: his prince decided kingdom father 
2013-02-21 16:06
uuien22
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-2-21
收藏
得分:0 
我对题目的理解:
王子被国王流放,起点是以“@”标记的土地,他可以在以“.”标记的土地走动,而以“#”标记的不可以。
求王子可以走动的土地的总数(包括“@”)。
2013-02-21 16:20
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:0 
你可以先描述你解决问题的大致思路,然后说一下你的代码出了哪些问题
这样别人会更容易去理解你的代码 也知道从哪里去找原因

人生是一场错过 愿你别蹉跎
2013-02-22 11:18
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:20 
关于这个问题 我有个相对简单一点的思路
就是从@出开始 把与他相邻的.全部标志成*
每变动一次 计数器加1 表示多了一个地方可以去 然后再由变为*的点开始 找与他相邻的. 做同样的动作
如果遇到#或*就退出
这个有点像漫水 从@处开始注水 有点的地方都漫到水 并继续向他周围有点的地方再漫 遇到#就不继续漫下去了
遇到*表示已经漫过水了 就不要再重复了
不知道对你有没有帮助 代码再下面 只测试过例子
程序代码:
#include<iostream>

using namespace std;

int N, W, H;

void ac_pos(char (*pos)[20], int i, int j, int &num)
{
    if(pos[i][j] == '.'){
        pos[i][j] = '*';
        num++;
        if(i > 0) ac_pos(pos, i-1, j, num);
        if(j > 0) ac_pos(pos, i, j-1, num);
        if(i < H-1) ac_pos(pos, i+1, j, num);
        if(j < W-1) ac_pos(pos, i, j+1, num);
    }
}


int main()
{
    char pos[20][20];
    int num, n = 0, srci, srcj;  //(srci, srcj) 记录@的位置    
    cin >> N;
    while(++n <= N){
        cin >> W >> H;
        for(int i(H); i > 0; i--)
            for(int j(0); j < W; j++){
                cin >> pos[i-1][j];
                if(pos[i-1][j] == '@'){
                    srci = i-1;
                    srcj = j;
                } 
            } 
        num = 0, pos[srci][srcj] = '.';
        ac_pos(pos, srci, srcj, num);
        cout << "Case " << n << ": " << num << endl;
    }
    
    return 0;
}

人生是一场错过 愿你别蹉跎
2013-02-22 11:31
uuien22
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-2-21
收藏
得分:0 
回复 3楼 fanpengpeng



 
以下是引用fanpengpeng在2013-2-22 11:18:17的发言:
你可以先描述你解决问题的大致思路,然后说一下你的代码出了哪些问题
这样别人会更容易去理解你的代码 也知道从哪里去找原因
    我是这样想的,输入所以的数据后,逐个坐标检查,如果是“.”的,就左右上下移动,遇到“#”停止,如果可以移到“@”标记的地方,计数的t加1。代码没有警告提示,但是运行的时候输入数据后会运行错误,电脑会要求关闭程序。
   
    (还有上面的代码,刚刚看了,这样把符合条件的位置计数后标记的办法更好,谢谢啦。)
2013-02-22 21:12
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:0 
用上下左右移动的方法 非常容易造成循环递归调用 导致栈空间耗尽 所以程序就被操作系统挂掉了
比如 .. 这样两个相邻的点 程序第一个.开始他会递归调用函数查找他右边第二个点 在第二点的时候 他又会递归调用去查找他左边第一个点
这样就造成了 无穷的循环递归 直至函数调用的层次把栈全耗尽 程序就中止了
我一开始也是直接想到这种方法的 只是还没有想到什么有效的解决方法 所以就想出了上面这么一个方法
不知道你有没有什么好的解决办法?

人生是一场错过 愿你别蹉跎
2013-02-23 08:43
uuien22
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-2-21
收藏
得分:0 
回复 6楼 fanpengpeng


    当时我也有担心会无限循环,所以就尝试用返回的数字来判断是否应该结束函数。不过还是不行
程序代码:
int pr( char (*a)[20] ,int n,int m)

 {
     if(a[n][m]=='@') {t++;return 0;}
     if(a[n][m+1]=='.' && m+1<=k)
         if(pr(a,n,m+1)==0) return 0;
     if(a[n][m-1]=='.' && m-1>=0)
         if(pr(a,n,m-1)==0) return 0;
     if(a[n-1][m]=='.' && n-1>=0)
         if(pr(a,n-1,m)==0) return 0;
     if(a[n+1][m]=='.' && n+1<=r)
         if(pr(a,n+1,m)==0) return 0;
     if(a[n][m+1]=='@' && m+1<=k) {t++;return 0;}
     if(a[n][m-1]=='@' && m-1>=0) {t++;return 0;}
     if(a[n-1][m]=='@' && n-1>=0) {t++;return 0;}
     if(a[n+1][m]=='@' && n+1<=r) {t++;return 0;}
     return 1;

 }

 





 
2013-02-23 15:42
Susake
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:女儿国的隔壁
等 级:贵宾
威 望:23
帖 子:2288
专家分:6481
注 册:2012-12-14
收藏
得分:0 
路过...

仰望星空...........不忘初心!
2013-02-23 16:08
快速回复:以下是我自己编的代码,但是找不出问题所在,大神求解
数据加载中...
 
   



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

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