POJ 2816 红与黑 解题报告
2009-02-05 23:24
344 查看
POJ 2816 红与黑 解题报告
编号:2816
考查点:递归
思路:主要的问题在于如何让其向不同方向进行递归,我的思路是另外开个变量,然后switch判断方向,结果自然而然的栈溢出了.。书上的方法确实经典,每次先加了,就不用考虑很多异常情况了,我的数组从1开始,事先全初始化为#,比书上代码又简洁了些.。
提交情况:自己的方法提交出现了多次RunTime Error.后来重写的代码还不错,轻松的AC了.。
Source Code:
//POJ Grids 2816
#include <iostream>
using namespace std;
int x,y;
char ch[25][25];
int Search(int m,int n)
{
if (ch[m]
=='#')
return 0;
else
ch[m]
='#';
return 1 + Search(m-1,n) + Search(m+1,n) + Search(m,n-1) + Search(m,n+1);
}
int main()
{
while (true)
{
cin>>x>>y;
if (x==0&&y==0)
break;
int m,n;
memset(ch,'#',sizeof ch);
for (int i=1;i<=y;i++)
{
for (int j=1;j<=x;j++)
{
cin>>ch[i][j];
if (ch[i][j]=='@')
{
m = i;
n = j;
}
}
}
int count = Search(m,n);
cout<<count<<endl;
}
return 0;
}
总结:表把问题想复杂了.。
By Ns517
Time 09.02.05
编号:2816
考查点:递归
思路:主要的问题在于如何让其向不同方向进行递归,我的思路是另外开个变量,然后switch判断方向,结果自然而然的栈溢出了.。书上的方法确实经典,每次先加了,就不用考虑很多异常情况了,我的数组从1开始,事先全初始化为#,比书上代码又简洁了些.。
提交情况:自己的方法提交出现了多次RunTime Error.后来重写的代码还不错,轻松的AC了.。
Source Code:
//POJ Grids 2816
#include <iostream>
using namespace std;
int x,y;
char ch[25][25];
int Search(int m,int n)
{
if (ch[m]
=='#')
return 0;
else
ch[m]
='#';
return 1 + Search(m-1,n) + Search(m+1,n) + Search(m,n-1) + Search(m,n+1);
}
int main()
{
while (true)
{
cin>>x>>y;
if (x==0&&y==0)
break;
int m,n;
memset(ch,'#',sizeof ch);
for (int i=1;i<=y;i++)
{
for (int j=1;j<=x;j++)
{
cin>>ch[i][j];
if (ch[i][j]=='@')
{
m = i;
n = j;
}
}
}
int count = Search(m,n);
cout<<count<<endl;
}
return 0;
}
总结:表把问题想复杂了.。
By Ns517
Time 09.02.05
相关文章推荐
- POJ 1003解题报告
- POJ 1004 Financial Management [解题报告] Java
- POJ 1005 I Think I Need a Houseboat [解题报告] Java
- poj1005 解题报告
- poj_1664_放苹果_解题报告
- poj 2152解题报告
- POJ1118 解题报告JAVA
- POJ2739解题报告
- POJ解题报告-小优
- poj2159解题报告
- poj1001解题报告
- POJ 2248解题报告
- POJ-1988-Cube Stacking 解题报告
- poj1142 解题报告
- POJ1250解题报告补
- POJ - 3087 Shuffle'm Up解题报告(模拟)
- POJ 2243解题报告
- POJ 2531 Network Saboteur解题报告(随机化算法)
- POJ-1142 & HDOJ-1333 Smith Numbers 解题报告
- POJ 1463 Strategic game (树形DP) 解题报告