积木覆盖 解题报告
2015-08-13 20:34
267 查看
没办法了我也只有这一种图片。。。凑合着看吧
不过是道水题,,各位神犇打眼一看就会了。。。
【解题思路】
深搜就好了。。每深搜一次就会搜掉一个积木,ans++;
只有10不会爆的。。
还有就是没有两块积木就公共边其实就是说深搜的时候可以往上下左右四个方向走,而不可以斜着走,,不难理解;
【代码】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int sx[4]={1,-1,0,0};
int sy[4]={0,0,1,-1};
int i,j,ans=0,a[100][100];
char ss[100];
void dfs(int s,int t)
{
int r,x,y;
for (r=0;r<4;++r)//向四个方向走
{
x=s+sx[r];
y=t+sy[r];
if (x>0&&y>0&&x<=10&&y<=10&&a[x][y]==1)//判断是否越界以及是否有积木
{
a[x][y]=0;
dfs(x,y);//无需回溯
}
}
}
int main()
{
freopen("puzzle.in","r",stdin);
freopen("puzzle.out","w",stdout);
for (i=1;i<=10;++i)
{
cin>>ss;
for (j=1;j<=10;++j)
a[i][j]=ss[j-1]-48;
}
for (i=1;i<=10;++i)
for (j=1;j<=10;++j)
if (a[i][j]==1)
{
ans++;//每深搜一次搜掉一块积木
a[i][j]=0;
dfs(i,j);
}
printf("%d",ans);
return 0;
}
【心得】
自己调试的时候出了一个小问题,,卡了半天,,,后来在发现算法并没有错,,而是调错了,,,这种情况就有去买一块豆腐的冲动。。。。。
相关文章推荐
- Android-应用开发-页面跳转和数据传递(六)
- Android:关于声明文件中android:process属性说明
- TCP连接
- 你真的理解了AsyncTask吗?
- 洛谷 P1858 多人背包
- Mac下安装MacProt,并GNU autotools的安装和使用 autoconf,automake
- C++, const:
- Web AppBuilder Developer 1.2优点与缺点
- Agri-Net
- Android-应用开发-网络编程(五)
- IOS--UI--NSOperation
- HDU 5386 Cover(暴力)
- swanzhu学ios(四)之UIScrollView与UIPageControl
- Agri-Net
- [leetcod] Valid Anagram 判断颠倒字母顺序而构成的单词
- HDU 3045 MAX Average Problem
- Android学习笔记(三)
- pscp+psftp+putty的使用
- HDU 1001 整型数据处理
- html与css经典二级菜单编写2