?? grid.cpp
字號:
/*
*author:renhao
*Id:063449
*time:2008.11.13
*version:1.0
*/
#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());
grid_file >> rows;
grid_file >> cols;
area = new vector<bool>(rows*cols, NOT_INFECTED);
flag = new vector<bool>(rows*cols, NOT_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();
}
// You may need to alter the destructor
grid::~grid () {
delete area;
}
// 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++) {
stream << ob.area->operator[](ob.indexof(row, col));
//if its flag changed then count"+ "
if(ob.area->operator[](ob.indexof(row,col))== INFECTED&&ob.flag->operator [](ob.indexof(row,col))==INFECTED)
cout<<"+ ";
else
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)
{
static int infec_num = 0;//used to count the infected number.
if(row>=0&&row<rows&&col>=0&&col<cols)//to ensure it is in the grid
{
//if it was infected and the flag is not changed
if(infected(row,col)==INFECTED&&flag->operator [](indexof(row,col)) ==NOT_INFECTED)
{
infec_num++;
flag->operator [](indexof(row,col)) = INFECTED;//to change the flag after it is been counted
count(row,col-1);//the 8 ones maybe affected by it
count(row,col+1);
count(row-1,col);
count(row+1,col);
count(row-1,col-1);
count(row-1,col+1);
count(row+1,col-1);
count(row+1,col+1);
}
}
return infec_num;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -