CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

高手指教:程序大赛题FireNet

楼主linlin2000(fdef)2005-04-03 23:27:33 在 C/C++ / C语言 提问

Firenet  
  Suppose   that   we   have   a   square   city   with   straight   streets.   A   map   of   a   city   is   a   square   board   with   n   rows   and   n   columns,   each   representing   a   street   or   a   piece   of   wall.    
   
  A   blockhouse   is   a   small   castle   that   has   four   openings   through   which   to   shoot.   The   four   openings   are   facing   North,   East,   South,   and   West,   respectively.   There   will   be   one   machine   gun   shooting   through   each   opening.    
   
  Here   we   assume   that   a   bullet   is   so   powerful   that   it   can   run   across   any   distance   and   destroy   a   blockhouse   on   its   way.   On   the   other   hand,   a   wall   is   so   strongly   built   that   can   stop   the   bullets.    
   
  The   goal   is   to   place   as   many   blockhouses   in   a   city   as   possible   so   that   no   two   can   destroy   each   other.   A   configuration   of   blockhouses   is   legal   provided   that   no   two   blockhouses   are   on   the   same   horizontal   row   or   vertical   column   in   a   map   unless   there   is   at   least   one   wall   separating   them.   In   this   problem   we   will   consider   small   square   cities   (at   most   4x4)   that   contain   walls   through   which   bullets   cannot   run   through.    
   
  The   following   image   shows   five   pictures   of   the   same   board.   The   first   picture   is   the   empty   board,   the   second   and   third   pictures   show   legal   configurations,   and   the   fourth   and   fifth   pictures   show   illegal   configurations.   For   this   board,   the   maximum   number   of   blockhouses   in   a   legal   configuration   is   5;   the   second   picture   shows   one   way   to   do   it,   but   there   are   several   other   ways.    
   
  此处图像无法粘贴  
   
  Your   task   is   to   write   a   program   that,   given   a   description   of   a   map,   calculates   the   maximum   number   of   blockhouses   that   can   be   placed   in   the   city   in   a   legal   configuration.    
   
  The   input   file   contains   one   or   more   map   descriptions,   followed   by   a   line   containing   the   number   0   that   signals   the   end   of   the   file.   Each   map   description   begins   with   a   line   containing   a   positive   integer   n   that   is   the   size   of   the   city;   n   will   be   at   most   4.   The   next   n   lines   each   describe   one   row   of   the   map,   with   a   '.'   indicating   an   open   space   and   an   uppercase   'X'   indicating   a   wall.   There   are   no   spaces   in   the   input   file.    
   
  For   each   test   case,   output   one   line   containing   the   maximum   number   of   blockhouses   that   can   be   placed   in   the   city   in   a   legal   configuration.    
   
  Sample   input:    
   
  4  
  .X..  
  ....  
  XX..  
  ....  
  2  
  XX  
  .X  
  3  
  .X.  
  X.X  
  .X.  
  3  
  ...  
  .XX  
  .XX  
  4  
  ....  
  ....  
  ....  
  ....  
  0  
   
  Sample   output:    
   
  5  
  1  
  5  
  2  
  4  
   
  简单的说,可以看成这样一个问题,一个N*N的棋盘,在上面尽可能多地放车,输入的  
  ...  
  .XX  
  .XX  
  中.为空格,X为一个屏障,同一行或列的两车之间如果有X的话便可以共存,求对任意输入的一个二维数组所代表的棋盘,所能放的车的最大数量  
  苦思良久,迫不得以才贴题求解,高手帮忙!  
   
  问题点数:0、回复次数:2Top

1 楼heroboy2000(动感超人)回复于 2005-04-04 07:33:38 得分 0

是acm题目吧  
  这道题以前好像做过Top

2 楼pcboyxhy(-273.15℃)回复于 2005-04-04 08:26:01 得分 0

#include<iostream.h>  
   
  char   *Street;  
  int   n,l;  
  int   MaxHouseNumber;  
   
  int   CanPut(int   X,   int   Y);  
  int   TryHouse(int   House,   int   X,   int   Y);  
   
  int   main(     )  
  {  
  int   Index=0,Answer[2000];  
  while(cin>>n)  
  {  
  if(!n)  
  break;  
  Street=new   char[n*n];  
  for(int   i=0;   i<n;   i++)  
  for(int   j=0;   j<n;   j++)  
  cin>>*(Street+i*n+j);  
  MaxHouseNumber=0;  
  l=TryHouse(0,   0,   0);  
  Answer[Index++]=MaxHouseNumber;  
  delete   Street;  
  }  
  for(int   i=0;   i<Index;   i++)  
  cout<<Answer[i]<<endl;  
  return   0;  
  }  
   
  int   TryHouse(int   House,   int   X,   int   Y)  
  {  
  int   IfCanPut;  
  if   (House>MaxHouseNumber)  
  MaxHouseNumber=House;  
  IfCanPut=CanPut(X,Y);  
  if(Y+1<n)  
  {  
  if(IfCanPut)  
  {  
  House++;  
  *(Street+n*X+Y)='O';  
  }  
  TryHouse(House,   X,   Y+1);  
  if(IfCanPut)  
  {  
  House--;  
  *(Street+n*X+Y)='.';  
  }  
  TryHouse(House,   X,   Y+1);  
  }  
  else   if(X+1<n)  
  {  
  if(IfCanPut)  
  {  
  House++;  
  *(Street+n*X+Y)='O';  
  }  
  TryHouse(House,   X+1,   0);  
  if(IfCanPut)  
  {  
  House--;  
  *(Street+n*X+Y)='.';  
  }  
  TryHouse(House,   X+1,   0);  
  }  
  else  
  {  
  if(IfCanPut)  
  House++;  
  if   (House>MaxHouseNumber)  
  MaxHouseNumber=House;  
  return   0;  
  }  
  }  
   
  int   CanPut(int   X,   int   Y)  
  {  
  if(*(Street+X*n+Y)!='.')  
  return   0;  
  for(int   b=X-1;   b>=0;   b--)  
  {  
  if(*(Street+b*n+Y)=='X')  
  break;  
  if(*(Street+b*n+Y)=='O')  
  return   0;  
  }  
  for(int   b=Y-1;   b>=0;   b--)  
  {  
  if(*(Street+X*n+b)=='X')  
  break;  
  if(*(Street+X*n+b)=='O')  
  return   0;  
  }  
  return   1;  
  }  
   
   
  ZJU   1002Top

相关问题

  • 2005ACM程序设计大赛预赛第一题,有兴趣过来看看啊
  • ACM程序设计大赛,西北工业大学预选赛的题目,有兴趣来看看
  • 广东大学生程序设计大赛中的一个题目,能否帮我优化一下???
  • 程序问题
  • 程序问题
  • java程序问题
  • c++程序问题?
  • c++程序问题?
  • 子程序问题
  • 子程序问题

关键词

  • blockhouses
  • maxhousenumber
  • street
  • canput
  • legal
  • wall
  • bullets
  • picture
  • city
  • board

得分解答快速导航

  • 帖主:linlin2000

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo