| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:请帮忙看看书上一道习题
只看楼主 加入收藏
姜倔倔
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-11-16
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:8 
请帮忙看看书上一道习题
图片附件: 游客没有浏览图片的权限,请 登录注册

这是题目。

8.9.c.zip (1.37 KB)

这是我写的代码,我这几天反反复复检查了十几遍,愣是没查出来错误在哪里。
请各位大神帮我看一看。

程序代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

#define N 10
#define CURRENTLETTER ch[x][y]
#define NEXTLETTER ch[x][y]+1
#define FILLER '.'

/**********************************

 * used to judge whether the      *

 * place is legal to put a letter *

 **********************************/
     
bool legal(int x, int y){
    char ch[x][y];
    if( ch[x][y]==FILLER &&
        x>0 && x<=10 &&
        y>0 && y<=10)
        return true;
    else return false;
}

int main(){
    char ch[N][N]={0};
    int x, y, movesTried = 0;
    
    for(x=1;x<=N;x++)
        for(y=1;y<=N;y++)
            ch[x][y]=FILLER;
    
    srand((unsigned)time(NULL));
    
    x=1;
    y=1;
    CURRENTLETTER='A';
    
    while(CURRENTLETTER < 'Z' && movesTried < 4){
        switch(rand()%4){
            case 0:
            if(legal(x+1,y)){
                x++ ;
                CURRENTLETTER = NEXTLETTER ;
                movesTried = 0;
            }else movesTried++;
            break;
        
            case 1:
            if(legal(x-1,y)){
                x-- ;
                CURRENTLETTER = NEXTLETTER ; 
                movesTried = 0;
            }else movesTried++;
            break;
            
            case 2:
            if(legal(x,y-1)){
                y-- ;
                CURRENTLETTER = NEXTLETTER ;
                movesTried = 0;
            }else movesTried++;
            break;
        
            case 3:
            if(legal(x,y+1)){
                y++;
                CURRENTLETTER = NEXTLETTER ;
                movesTried = 0;
            }else movesTried++;
            break;
        }
    }
    
    for(x=1;x<=N;x++){
        for(y=1;y<=N;y++){
            printf("%c ",CURRENTLETTER);
        }
        printf("\n");
    }
    
    return 0;
}


[ 本帖最后由 姜倔倔 于 2013-11-20 18:00 编辑 ]
搜索更多相关主题的帖子: color 
2013-11-20 16:54
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:10 
草...   我的VC6.0不支持  bool 型   你的支持?

三十年河东,三十年河西,莫欺少年穷!
2013-11-20 17:25
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:10 
代码怎不直接粘上来?
2013-11-20 17:52
姜倔倔
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-11-16
收藏
得分:0 
回复 3楼 heroinearth
粘贴上来了 求教
2013-11-20 18:00
姜倔倔
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-11-16
收藏
得分:0 
回复 2楼 韶志
bool是c99支持的。。。
2013-11-20 18:00
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
for(x=1;x<=N;x++)
        for(y=1;y<=N;y++)
            ch[x][y]=FILLER;
x,y=10时数组已经出界,

这是我修改后的:
程序代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//#include <stdbool.h>

#define N 10
#define CURRENTLETTER ch[x][y]
#define NEXTLETTER (++current)
#define FILLER '.'

/**********************************
* used to judge whether the      *
* place is legal to put a letter *
**********************************/
    
int legal(char ch[][N],int x, int y){
    //char ch[x][y];
    if(ch[x][y]==FILLER && x>=0 && x<10 && y>=0 && y<10)
        return 1;
    else return 0;
}

int noway(char ch[][N],int x,int y)//判断下一步是否还有路可走,无路返回1
{
    if(x==0 && y>0 && y<9)//上边
    {
        if(ch[x+1][y]==FILLER || ch[x][y-1]==FILLER || ch[x][y+1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x==0 && y==9)//右上角
    {
        if(ch[x][y-1]==FILLER || ch[x+1][y-1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x>0 && x<9 && y==9)//右边
    {
        if(ch[x][y-1]==FILLER || ch[x-1][y]==FILLER || ch[x+1][y]==FILLER)
            return 0;
        else
            return 1;
    }
    else if(x==9 && y==9)//右下角
    {
        if(ch[x-1][y]==FILLER || ch[x][y-1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x==9 && y>0 && y<9)//下边
    {
        if(ch[x-1][y]==FILLER || ch[x][y-1]==FILLER || ch[x][y+1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x==9 && y==0)//左下角
    {
        if(ch[x-1][y]==FILLER || ch[x][y+1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x>0 && x<9 && y==0)//左边
    {
        if(ch[x+1][y]==FILLER || ch[x-1][y]==FILLER || ch[x][y-1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x==0 && y==0)//左上角
    {
        if(ch[x+1][y]==FILLER || ch[x][y+1]==FILLER)
            return 0;
        else
            return 1;
    }else if(x>0 && x<9 && y>0 && y<9)//中间
    {
        if(ch[x+1][y]==FILLER || ch[x-1][y]==FILLER || ch[x][y+1]==FILLER || ch[x][y-1]==FILLER)//中间
            return 0;
        else
            return 1;
    }
    return 2;
}

   
   
    

int main(void){
    char ch[N][N], current='A';
    int x, y, movesTried = 0;
   
    for(x=0;x<N;x++)
        for(y=0;y<N;y++)
            ch[x][y]=FILLER;
   
    srand((unsigned)time(NULL));
   
    x=0;
    y=0;
   
    CURRENTLETTER='A';
    while(current < 'Z'){
        switch(rand()%4){
            case 0:
            if(legal(ch,x+1,y)){
                x++ ;
                CURRENTLETTER = NEXTLETTER ;
                //movesTried = 0;
            }//else movesTried++;
            break;
       
            case 1:
            if(legal(ch,x-1,y)){
                x-- ;
                CURRENTLETTER = NEXTLETTER ;
               // movesTried = 0;
            }//else movesTried++;
            break;
           
            case 2:
            if(legal(ch,x,y-1)){
                y-- ;
                CURRENTLETTER = NEXTLETTER ;
                //movesTried = 0;
            }//else movesTried++;
            break;
       
            case 3:
            if(legal(ch,x,y+1)){
                y++;
                CURRENTLETTER = NEXTLETTER ;
                //movesTried = 0;
            }//else movesTried++;
            break;
        }
        if(noway(ch,x,y))
            break;
    }
   
    for(x=0;x<N;x++){
        for(y=0;y<N;y++){
            printf("%c ",CURRENTLETTER);
        }
        printf("\n");
    }
   
    return 0;
}



[ 本帖最后由 heroinearth 于 2013-11-20 19:33 编辑 ]
2013-11-20 18:16
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
运行结果
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册
2013-11-20 20:03
姜倔倔
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2013-11-16
收藏
得分:0 
回复 6楼 heroinearth
我觉得问题可能出在legal()函数的写法上

我参考了您的代码,把legal()函数改成了

图片附件: 游客没有浏览图片的权限,请 登录注册
就ok了

您能帮我解释一下为什么之前在函数体里写char ch[x][y]不对呢?

---
然后我考虑了一下您的代码,我觉得不需要那么细致地讨论noway的情况,只需要在上下左右全部!legal()的时候break就好了
程序代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

#define N 10
#define CURRENTLETTER ch[x][y]
#define NEXTLETTER (++current)
#define FILLER '.'

/**********************************

 * used to judge whether the      *

 * place is legal to put a letter *

 **********************************/
     
bool legal(char ch[N][N], int x, int y){
    if( ch[x][y]==FILLER &&
        x>=0 && x<N &&
        y>=0 && y<N)
        return true;
    else return false;
}

int main(){
    char ch[N][N]={0},current='A';
    int x, y;
    
    for(x=0;x<N;x++)
        for(y=0;y<N;y++)
            ch[x][y]=FILLER;
    
    srand((unsigned)time(NULL));
    
    x=0;
    y=0;
    CURRENTLETTER='A';
    
    while(CURRENTLETTER < 'Z'){
        if(    legal(ch,x+1,y) || legal(ch,x-1,y) ||
            legal(ch,x,y+1) || legal(ch,x,y-1)){
            switch(rand()%4){
            case 0:
            if(legal(ch, x+1,y)){
                x++ ;
                CURRENTLETTER = NEXTLETTER ;
            }
            break;
        
            case 1:
            if(legal(ch, x-1,y)){
                x-- ;
                CURRENTLETTER = NEXTLETTER ; 
            }
            break;
            
            case 2:
            if(legal(ch, x,y-1)){
                y-- ;
                CURRENTLETTER = NEXTLETTER ;
            }
            break;
        
            case 3:
            if(legal(ch, x,y+1)){
                y++;
                CURRENTLETTER = NEXTLETTER ;
            }
            break;
            }
        }
        else break;
    }
    
    for(x=0;x<N;x++){
        for(y=0;y<N;y++){
            printf("%c ",CURRENTLETTER);
        }
        printf("\n");
    }
    
    return 0;
}
2013-11-20 22:55
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
回复 8楼 姜倔倔
因为在函数体内定义的数组是局部变量,里面存的不可能是main中数组的数据

是我想的太多,走了弯路!

[ 本帖最后由 heroinearth 于 2013-11-21 09:29 编辑 ]
2013-11-21 09:06
快速回复:请帮忙看看书上一道习题
数据加载中...
 
   



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

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