回溯搜索166:The Castle(OpenJudge…

166:The Castle

时间限制: 
1000ms
  内存限制: 
65536kB
描述
     1   2   3   4   5   6   7  
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(Figure 1)

# = Wall
| = No wall
- = No wall

Figure 1 shows the map of a castle.Write a program that calculates
1. how many rooms the castle has
2. how big the largest room is
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls. 
输入
Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.
输出
Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).
样例输入
4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
样例输出
5
9
源码(OpenJudge提交通过)
#include"stdio.h"
#include"stdlib.h"

struct module
{
char north;
char south;
char east;
char west; //上下左右四面墙 
int sum;
int sr;
int sc; // (sr,sc)记录到达此位置的上一个位置,以便返回 
bool flag;
};

int main()
{
int i,j,m , n; //m行,n列
scanf("%d",&m);
scanf("%d",&n);
struct module md[50][50];
//struct module **md=(struct module**)malloc(sizeof(struct module)*m); 
for(i=0;i<m;i++)
{
/
if(md[i][j].sum%2==1)
md[i][j].west='y';
else
md[i][j].west='n';
md[i][j].sum/=2;
if(md[i][j].sum%2==1)
md[i][j].north='y';
else
md[i][j].north='n';
md[i][j].sum/=2;
if(md[i][j].sum%2==1)
md[i][j].east='y';
else
md[i][j].east='n';
md[i][j].sum/=2;
if(md[i][j].sum%2==1)
md[i][j].south='y';
else
md[i][j].south='n';
md[i][j].flag=false;
}
}
int r , c; //记录当前搜索的行和列 
int NumRoom ,MaxRoom,temp,count;
count=0; //标记被占有的module 
NumRoom=0;
MaxRoom=0;
bool fg;
while(count<m*n)
{
fg=false;
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{ if(!md[r][c].flag)
{
fg=true;
break;
}
}
if(fg)
break;
}

md[r][c].flag=true;
count++;
temp=1;
i=r;
j=c; //记录开始搜索的位置
md[r][c].sr=r;
md[r][c].sc=c;
int si,sj;
int redo=0;//
do{
if(md[r][c].west=='n' && c>0 && !md[r][c-1].flag)
{
md[r][c-1].sr=r;
md[r][c-1].sc=c;
c--;
count++;
temp++; 
md[r][c].flag=true;
}//左走,west
else if(md[r][c].north=='n' && r>0 && !md[r-1][c].flag)
{
md[r-1][c].sr=r;
md[r-1][c].sc=c;
r--;
count++;
temp++; 
md[r][c].flag=true;
}//上走,north
else if(md[r][c].east=='n' && c<n-1 && !md[r][c+1].flag)
{
md[r][c+1].sr=r;
md[r][c+1].sc=c;
c++;
count++;
temp++; 
md[r][c].flag=true;
}//右走,east
else if(md[r][c].south=='n' && r<n-1 && !md[r+1][c].flag)
{
md[r+1][c].sr=r;
md[r+1][c].sc=c;
r++;
count++;
temp++; 
md[r][c].flag=true;
}//下走,south
else{
si=md[r][c].sr;
sj=md[r][c].sc;
r=si;
c=sj;
}//返回上一个位置 
if(r==i && c==j)
redo++; //在每个开始搜索的位置重复4次
}while(redo<4 );
NumRoom++;
if(temp>MaxRoom)
MaxRoom=temp;
}
printf("%d\n%d\n",NumRoom,MaxRoom);
return 0;
}
运行结果
回溯搜索166:The <wbr>Castle(OpenJudge)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值