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

大家帮我看下代码最后的释放二维数组部分有什么问题吧 谢谢了

Posted by dalang0407 at 2010-06-03 17:19:02 on Problem 1573
#include <iostream>
using namespace std;

void move(int** grid, int &r, int &c, int** visited)
{
	switch(grid[r][c])
	{
	case 0:r -= 1;break;
	case 1:c += 1;break;
	case 2:r += 1;break;
	case 3:c -= 1;break;
	}
	visited[r][c]++;
}

void solve(int** grid, int r, int c, int entry, int** visited)
{
	int curr = 0;
	int curc = entry - 1;
	visited[curr][curc] = 1;
	int cnt = 0;
	while(1)
	{
		move(grid, curr, curc, visited);
		cnt++;
		if (curr < 0 || curr >= r || curc < 0 || curc >= c)
		{
			cout << cnt << " step(s) to exit" << endl;
			return;

		}
		if (visited[curr][curc] == 2)
		{
			int loopcnt = 0;
			int tempx = curr;
			int tempy = curc;
			while(visited[curr][curc] != 3)
			{
				move(grid, curr, curc, visited);
				loopcnt++;
			}
			cout << cnt - loopcnt << " step(s) before a loop of " << loopcnt << " step(s)"<<endl;
			return;
		}
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	int** grid;
	int** visited;
	int r, c, entry;
	int i, j;
	char dir;
	while((cin >> r >> c >> entry) && (r != 0) && (c != 0) && (entry != 0))
	{
		grid = new int*[r];
		visited = new int*[r];
		for (i = 0; i < r; i++)
		{
			grid[i] = new int[c];
			visited[i] = new int[c];
			for (j = 0; j < c; j++)
			{
				visited[i][j] = 0;
				cin >> dir;
				switch(dir)
				{
				case 'N':grid[i][j] = 0;break;
				case 'E':grid[i][j] = 1;break;
				case 'S':grid[i][j] = 2;break;
				case 'W':grid[i][j] = 3;break;
				}
			}
		}
		
		solve(grid, r, c, entry, visited);
		for (i = 0;i < r; i++)
		{
			delete []grid[i];
			delete []visited[i];
		}
		delete []grid;
		delete []visited;
	}
	return 0;
}

RT 最后释放那块有问题 拿题目的第一组数据 就只能i=0 1 的时候释放 此时r=3 i++后变为2 结果就停那不动了。。。
而第二组数据可以释放 
最后的结果是RE 。。没找出原因。。

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