Programming Problem: Influenza infection
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 1000ms memory limit: 65536kB
Describe
There are a group of susceptible people living in the grid-like dormitory area, the dormitory area for the n*n matrix, each lattice point for a room, the room may live people, may also be empty. On the first day, some of the people in the room got the flu, and every day, people who get the flu will infect their neighbours with the flu, and the empty room will not infect. Please output the number of people who have been vaccinated for the first day.
Input
The first line of a number n,n no more than 100, indicating that there are n*n dorm rooms.
Next n lines, n characters per line, '. ' said that the first day of the room to live in a healthy person, ' # ' means that the room is empty, ' @ ' means the first day of the room to live with the flu people.
The next line is an integer m,m not more than 100.
Output
Output m days, number of people getting influenza
Sample input
5 ..... #[email protected]. #@.. #.... ..... 4
Sample output
16
Idea: Each day will mark the person infected, and mark the next day the person who will be infected
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5 intMain ()6 {7 Chara[101][101];8 intN, m, sum =0;9CIN >>N;Ten for(inti =0; i < n; i++) One { A for(intj =0; J < N; J + +) - { -CIN >>A[i][j]; the } - } -CIN >>m; - for(intD =1; D <= m; d++) + { - for(inti =0; I < n; i++) + { A for(intj =0; J < n;j++) at { - if(A[i][j] = ='!') -A[I][J] ='@';//infected with the person marked the day before - } - } - for(inti =0; I < n; i++) in { - for(intj =0; J < n;j++) to {
33//Mark people around infected people + if(A[i][j] = ='@') - { the if(i +1<n && A[i +1][J] = ='.') *A[i +1][j]='!'; $ if(J-1>=0&& A[i][j-1] =='.')Panax NotoginsengA[i][j-1] ='!'; - if(j +1< n && a[i][j +1] =='.') theA[i][j +1]='!'; + ifI1>=0&& A[i-1][J] = ='.') AA[i-1][J] ='!'; the } + } - } $ $ } - for(inti =0; I < n; i++) - { the for(intj =0; J < N; J + +) - {Wuyi if(A[i][j] = ='@') thesum++; - } Wu } -cout << sum <<Endl; About return 0; $}
POJ C Programming Advanced Programming Problem #: Influenza infection