Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为什么会Runtime Error?

Posted by 3013216027 at 2014-07-22 09:07:19 on Problem 2386
In Reply To:为什么会Runtime Error? Posted by:shuangkouchengzhi at 2014-04-29 16:41:56
> #include <iostream>
> using namespace std;
> void check(char**p,int,int);
> int main()
> {
>     int N,M;
>     cin>>M>>N;
>     char**p=new char*[N];
>     for (int i=0;i<N;i++) p[i]=new char[M];
>     for (int j=0;j<N+2;j++)
>     {
>         p[0][j]='.';
>         p[M+1][j]='.';
>     }
>     for (int i=1;i<M+1;i++)
>     {
>         p[i][0]='.';
>         p[i][N+1]='.';
>     }
>     for (int i=1;i<=M;i++)
>         for (int j=1;j<=N;j++)
>             cin>>p[i][j];
>     int counter=0;
>     for(int i=1;i<=M;i++)
>         for(int j=1;j<=N;j++)
>             if(p[i][j]=='W')
>             {
>                 counter++;
>                 p[i][j]=1;
>                 check(p,i,j);
>             }
>     cout<<counter<<endl;
>     for (int i=0;i<N;i++) delete[] p[i];
>     delete[] p;
>     return 0;
> 
> }
> 
>  void check(char**p,int hang,int lie)
> {
>     bool flag=true;
>     while(flag)
>     {
>         flag=false;
>         if (p[hang-1][lie+1]=='W')
>         {
>             flag=true;
>             p[hang-1][lie+1]=1;
>             check(p,hang-1,lie+1);
>         }
>         for(int i=hang;i<=hang+1;i++)
>             for(int j=lie-1;j<=lie+1;j++)
>             {
>                 if(p[i][j]=='W')
>                 {
>                     flag=true;
>                     p[i][j]=1;
>                     check(p,i,j);
>                 }
>             }
>     }
>     if(flag==false) return;
> 
> }
> 
话说这题100字符而已,你动态分配不仅节约不了多少内存,反而代码量增加,何苦呢.而且即使要
动态分配,还是要多分配一点的...留点余地,这可以增强程序的鲁棒性.有些题目的数据可能是有缺陷的,比如要求100,但是它给了101,,要是你不知情,怎么破?

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator