标签:
#ifndef GRID_H
#define GRID_H
#include <string>
#include <vector>
using namespace std;
/*
* IMPORTANT NOTE:
*
* For this assignment, you might need to add state to the
* class and/or argment existing methods, and/or create private helper
* methods, but you should not delare new public methods
*/
const bool INFECTED = true;
const bool NOT_INFECTED = false;
class grid;
class grid {
private:
int rows;
int cols;
int num;
vector<bool> *area;
vector<bool> *mark;
int indexof(int row, int col) const;
bool infected(int row, int col) const;
public:
struct offsets
{
int x, y;
char* dir;
};
offsets pace[8] = { { -1,0,"N" },{ -1,1,"NE" },{ 0,1,"E" },{ 1,1,"SE" },{ 1,0,"S" },{ 1,-1,"SW" },{ 0,-1,"W" },{ -1,-1,"NW" } };
offsets downpace[5] = { {0,1},{1,1},{1,0},{1,-1},{0,-1} };
offsets uppace[5] = { {-1,0},{-1,1},{0,1},{0,-1},{-1,-1} };
offsets rightpace[5] = { {-1,0},{-1,1},{0,1},{1,1},{1,0} };
offsets leftpace[5] = { {-1,0},{1,0},{1,-1},{0,-1},{-1,-1} };
offsets nwpace[3] = { {0,1},{1,1},{1,0} };
grid(string file);
~grid();
int count(int row, int col);
friend ostream &operator<<(ostream &stream, const grid& ob);
};
#endif
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
#include "grid.h"
// You do not need to alter function indexof.
int grid::indexof(int row, int col) const {
return row*cols + col;
}
// You do not need to alter function infected.
bool grid::infected(int row, int col) const {
return (area->operator[](indexof(row, col)) == INFECTED);
}
// You may need to alter the constructor
grid::grid(string file) {
ifstream grid_file;
grid_file.open(file.c_str());
if (!grid_file)
{
cerr << "can not open this file" << endl;
return;
}
grid_file >> rows;
grid_file >> cols;
area = new vector<bool>(rows*cols, NOT_INFECTED);
mark = new vector<bool>(rows*cols, INFECTED);
while (true) {
int blob_row;
int blob_col;
grid_file >> blob_row;
grid_file >> blob_col;
if (grid_file.eof()) {
break;
}
area->operator[](indexof(blob_row, blob_col)) = INFECTED;
}
grid_file.close();
num = 0;
}
// You may need to alter the destructor
grid::~grid() {
delete area;
delete mark;
}
// You will need to alter this function to display the
// plus signs (+) next to the cells that belong to
// a counted colony.
ostream &operator<<(ostream &stream, const grid& ob) {
for (int row = 0; row < ob.rows; row++) {
for (int col = 0; col < ob.cols; col++) {
if (ob.mark->operator[](ob.indexof(row, col)) == NOT_INFECTED)
{
stream << ob.area->operator[](ob.indexof(row, col))<<"+";
stream << " ";
}
else
{
stream << ob.area->operator[](ob.indexof(row, col));
stream << " ";
}
}
stream << endl;
}
stream << endl;
return stream;
}
// Replace the return statement in this function with your
// recursive implementation of this method */
int grid::count(int row, int col) {
if (row == 0 && col == 0)//右上角的顶点,其他三个顶点类似,就不再写了。。。。
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 3; i++)
{
row += nwpace[i].x;
col += nwpace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);
}
else
{
row = row - nwpace[i].x;
col = col - nwpace[i].y;
}
}
}
if (row == 0 && col != 0)//到达第一行时
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += downpace[i].x;
col += downpace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);
}
else
{
row = row - downpace[i].x;
col = col - downpace[i].y;
}
}
}
if (row == 5)//到达最后一行时
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += uppace[i].x;
col += uppace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);
}
else
{
row = row - uppace[i].x;
col = col - uppace[i].y;
}
}
}
if (col == 0 && row != 0)
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += rightpace[i].x;
col += rightpace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);
}
else
{
row = row - rightpace[i].x;
col = col - rightpace[i].y;
}
}
}
if (col == 5)
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += leftpace[i].x;
col += leftpace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);
}
else
{
row = row - leftpace[i].x;
col = col - leftpace[i].y;
}
}
}
if (row != 0 && row != 5 && col != 0 && col != 5)//中间的部分
{
if (area->operator[](indexof(row, col)) == true)//如果改点被感染了
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;//标记
num++;
for (int i = 0; i < 8; i++)//从八个方向去试探
{
row += pace[i].x;
col += pace[i].y;
if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{
count(row, col);//递归
}
else//如果改点行不通就回溯
{
row = row - pace[i].x;
col = col - pace[i].y;
}
}
}
}
return num;
}
C++ ssd5 16 optional exercise 3
标签:
原文地址:http://www.cnblogs.com/bigminnow/p/5350807.html